jquery Sortable connectWith两次调用update方法

Abh*_*ain 29 jquery jquery-ui-sortable

在下面的更新功能的代码,当一个项目从列表中移出被调用两次sortable1sortable2.虽然我只需要调用一次该函数:

$("#sortable1 tbody, #sortable2 tbody").sortable({
    connectWith: '.connectedSortable tbody',
    helper: fixHelper,
    handle : '.handle',
    update : function () {
        var order = $('#sortable1 tbody').sortable('serialize');
    }    
}).disableSelection();
Run Code Online (Sandbox Code Playgroud)

Ste*_*fan 58

答案来自:http://forum.jquery.com/topic/sortables-update-callback-and-connectwith

update: function(e,ui) {
    if (this === ui.item.parent()[0]) {
        //your code here
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 值得注意的是,"这个"在这里是有趣的价值.`update`被触发两次,即被拖出元素的列表和被拖入元素的列表.`ui.item.parent()`指的是被拖动元素的父元素.如果你想知道为什么这个有用:) (8认同)

use*_*ser 11

Stefan的答案很好,但是它没有提到另外一个难题,所以这里是 - 如果有人(像我一样)不能马上得到它.这应该使您可以在update()函数中处理所有这些并且不必弄乱receive()(只有在容器间运动发生时才会触发):

update: function(e,ui) {
    if (this === ui.item.parent()[0]) {
        if (ui.sender !== null) {
          // the movement was from one container to another - do something to process it
          // ui.sender will be the reference to original container
        } else {
          // the move was performed within the same container - do your "same container" stuff
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


Cod*_*ger 6

试试这个:

update: function(e,ui) {
    if (!ui.sender) {
        //your code here
    }
}
Run Code Online (Sandbox Code Playgroud)


Bra*_*ler 5

您应该使用接收事件(http://jqueryui.com/demos/sortable/#event-receive)。

http://bugs.jqueryui.com/ticket/3178 的最底部查看分辨率。


Jon*_*let 1

我刚刚遇到了这个。这是 jQuery UI 中的一个错误,请参阅http://bugs.jqueryui.com/ticket/4872#comment:2

我发表评论是为了看看是否可以叫醒任何人,让他们知道何时会有修复。社区驱动发展的乐趣 :P