move draggable between droppable + sortable containers

风流意气都作罢 提交于 2020-01-06 08:15:24

问题


I've got a scenario where I have multiple droppable containers, that are also sortable. I need to be able to drag clones from my source draggable, and drop them on any of the droppables. Within each droppable, the items need to be sortable. I also need to be able to drag a draggable from one droppable to another.

Here is a JSFiddle I've started as a proof of concept that I can't quite get working.

There are two issues I can see:

  1. If you drag two items onto the first (left-most) droppable container, and then drag the bottom one up to the top (to re-sort them), you'll notice an animation coming from the source draggable up top. I assume this has something to do with the clone but I wasn't able to find the culprit.

  2. With the two items on the first (left-most) droppable container, if you try and drag one of the items over to the middle droppable, you see it revert again animate back from the source draggable up top.

I hope I'm not making it overly complex but in the drop function I'm using the dropped class to tell me whether the item has been dropped or not before, and if it doesn't have the class I know it's the first time dropping , hence creating the clone and appending it to the droppable.

Otherwise, if the draggable has been dropped (i.e. has the class dropped), and changed droppables, I won't re-clone it but append it to the new droppable.

if(ui.draggable.hasClass("dropped") && DifferentSource()){
   // this item was already dropped, just changed sources:
   ui.draggable.appendTo($(this));
} else if(ui.draggable.hasClass("dropped") == false) {
   var clone = ui.draggable.clone();
   count += 1;
   clone.html("item " + count);
   clone.addClass("dropped");
   clone.appendTo($(this));
}

Can anyone see what I'm doing wrong?


回答1:


The missing key was sortable's connectWith property. This allowed me to connect the different droppables so that sortable doesn't revert back when changing droppables.

Here is the working fiddle. Hopefully this saves someone time in the future!



来源:https://stackoverflow.com/questions/21639020/move-draggable-between-droppable-sortable-containers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!