7

在 JqueryUI sortable 中,当用户拖动一个项目时,调用 change 事件。我在下面的例子中得到了这个。

jsFiddle 代码在这里

stop: function(event, ui) {
        console.log("New position: " + ui.item.index());
    },
    change: function(event, ui) {
        // When the item postion is changed while dragging a item, I want it's position
        console.log("New position: " + ui.item.index());
    }

在我的代码中,当用户拖动一个项目(Item7)并更改位置时,同时拖动一个项目并更改位置。我想获得拖动项目的位置。

我刚刚在这里看到了一个答案:jQuery UI Sortable Position

在上面的链接中,位置值(ui.item.index() )是在停止功能上访问的,在拖动项目时,我想要它的位置(在更改功能上)?任何想法 ?

我需要这个,因为当用户尝试在 item2 之后放置 item7 时,我会做一些验证,从前一个 Item2 获取值(如果我知道拖动项目的当前位置,那么只有我可以访问前一个项目)。如果验证成功,可以放置它或者我必须显示一些消息。

4

2 回答 2

8

如果您想获取当前悬停的索引,请尝试使用placeholder.

change: function(event, ui) {
      console.log("New position: " + ui.placeholder.index());
}

小提琴

于 2013-06-18T06:05:17.350 回答
0

裘德杜兰的答案不会一直有效(在我的头发去皮后发现了这一点)。第一个和最后一个项目占位符根据鼠标位置移动。

function getPaceholderIndex( ui ) {
    var clone = $(myMenu).clone();
    $('.ui-sortable-helper', clone).remove();
    return $('.ghost', clone).index();
}

将更有效地工作,因为它首先删除实际项目并离开占位符。请注意,我将“ghost”设置为占位符参数。

于 2014-07-16T05:32:13.997 回答