Vue.Draggable icon indicating copy to clipboard operation
Vue.Draggable copied to clipboard

Question: is it possible to modify the prop data for a component being dragged when it's added to a list?

Open chris-schmitz opened this issue 9 years ago • 5 comments

I'm creating an application that shows a list of possible commands and allows the user to drag those commands to a separate list to build up a "program".

screen shot 2017-08-09 at 1 46 35 pm

The list to list move works fine due to vue.draggable (thanks :) ).

The data for the two lists looks like this in my vue component:

// the list of possible commands:
availableCommands: [
  { id: 1, iconClass: 'fa-arrow-up', label: 'forward', commandLabel: "FORWARD", duration: '1000'},
  { id: 2, iconClass: 'fa-arrow-down', label: 'reverse', commandLabel: "REVERSE", duration: '1000'},
  { id: 3, iconClass: 'fa-arrow-left', label: '', commandLabel: "FORWARDLEFT", duration: '1000'},
  { id: 4, iconClass: 'fa-arrow-right', label: '', commandLabel: "FORWARDRIGHT", duration: '1000'},
  { id: 5, iconClass: 'fa-rocket', label: '', commandLabel: "TAKEOFF", duration: '1000'},
  { id: 6, iconClass: 'fa-undo', label: '', commandLabel: "ROTATELEFT", duration: '1000'},
  { id: 7, iconClass: 'fa-repeat', label: '', commandLabel: "ROTATERIGHT", duration: '1000'},
  { id: 8, iconClass: 'fa-stop-circle', label: '', commandLabel: "STOP", duration: '1000'},
  { id: 9, iconClass: 'fa-exchange', label: '', commandLabel: "SHAKE", duration: '1000'}
]

// the list of commands dragged into the bottom list:
commandsInProgram: [],

One of the things I'm trying to do is add a couple of properties to the component data that represents a particular step. I was reading through the docs for vue.draggable and sortable.js and saw that there was an add event that I can hook into. I added an event hook to my bottom list:

<draggable class='drag-area' id="commandsInProgram" v-model="commandsInProgram" :options="{group: 'commands'}" @add="addFieldsToProgramStep">

And then added a method to handle the event:


    addFieldsToProgramStep (event) {
      debugger
      console.log(event.item)
    },

I have access to the markup and index information for the item, but I don't see how I can use this to modify the data that will eventually be added to the commandsInProgram array. I'm wanting to add complete: false and programId: <current program index value> to the data.

I'm still reading through the docs, maybe it's spelled out there and I just didn't see it, but it seems like this may be a common need so I thought I'd ask here too.

Thanks! this package made building out this app a ton easier and I appreciate the work you all have put into it.

chris-schmitz avatar Aug 09 '17 18:08 chris-schmitz

Hello @chris-schmitz Did you check the clone props

David-Desmaisons avatar Aug 09 '17 19:08 David-Desmaisons

Ah! no I hadn't seen that. So on my component being dragged, I can add a method named clone() and it will be called automatically any time an instance of that component is cloned into a new list, am I understanding that correctly?

chris-schmitz avatar Aug 09 '17 20:08 chris-schmitz

Aaaah ok, so looking at the example files:

https://github.com/SortableJS/Vue.Draggable/blob/master/examples/Two_Lists_Clone.html

and

https://github.com/SortableJS/Vue.Draggable/blob/master/examples/script/complex.js

it looks like I need to add the pull: 'clone' option to the bound options attribute in the markup on the source list:

<draggable id="list1" :list="list1" class="dragArea" :options="{group:{name:'people', pull:'clone', put:false }}" @change="log">

And then add the clone method to the vm:

methods:{
  ...
  clone: function(el){
    return {
      name : el.name + ' cloned'
    }
  },
  ...
}

Is that correct?

chris-schmitz avatar Aug 09 '17 20:08 chris-schmitz

Yes you are correct. Note that clone is called on dragstart on the source draggable component

David-Desmaisons avatar Aug 09 '17 20:08 David-Desmaisons

Perfect. I'll give it a try tonight. Thanks for the fast replies! 👏

chris-schmitz avatar Aug 09 '17 21:08 chris-schmitz