Search code examples
javascriptvuejs2vuetify.jsvuedraggable

Implementing Vue draggable


i am trying to implement vue draggable and it almost seems to work except for when i try to implement it on a button. It gives me an error message whenever i try to move the button.

Here is an example : https://codepen.io/anon/pen/xoQRMV?editors=1111

          <div id="app">
    <v-app id="inspire">
      <v-container>
        <v-layout justify-center>
     <v-flex>
       <draggable v-model="myArray" :options="options" handle=".handle">    
          <div v-for="element in myArray" :key="element.id" class="title 
        mb-3">{{element.name}}
             <v-icon color="red" class="handle mt-0">drag_handle</v-icon>
           </div>   
          <v-btn class="ml-0">Button</v-btn>
          <v-icon color="red" class="handle">drag_handle</v-icon>
         </draggable>
        </v-flex>
       </v-layout>
   </v-container>
     </v-app>
   </div>


        new Vue({
    el: '#app',
     data() {
     return {
      myArray: [
       {name: 'Text1!!!!', id: 0},
       {name: 'Text2!!!!', id: 1},
         ],
         options: {
        handle: '.handle'
        }
            }
           }
          })

Any help is appreciated.


Solution

  • It would have to work from a single array I think, e.g. https://codepen.io/anon/pen/agQVvm?editors=1111

    <div id="app">
      <v-app id="inspire">
         <v-container>
           <v-layout justify-center>
             <v-flex>
               <draggable :list="combinedArray" :options="options" handle=".handle">    
                 <div v-for="element in combinedArray" :key="element.id" class="title mb-3">
                   <div v-if="element.type !== 'button'" class="title mb-3">
                     {{ element.name }}
                     <v-icon color="red" class="handle mt-0">drag_handle</v-icon>
                   </div>
    
                   <div v-else>
                     <v-btn>{{ element.name }}</v-btn>
                     <v-icon color="red" class="handle mt-0">drag_handle</v-icon>
                   </div>
                 </div>  
               </draggable>
             </v-flex>
           </v-layout>
      </v-container>
      </v-app>
    </div>
    
    new Vue({
      el: '#app',
    
      created () {
        this.combinedArray = [...this.myArray, ...this.buttonsArray]
      },
    
      data () {
        return {
          myArray: [
            { name: 'Text1!!!!', id: 0 },
            { name: 'Text2!!!!', id: 1 }
          ],
          buttonsArray: [
            { name: 'Button1', id: 2, type: 'button' },
            { name: 'Button2', id: 3, type: 'button' }
          ],
          combinedArray: [],
          options: {
            handle: '.handle'
          }
        }
      }
    })