更改事件上的Jquery UI可排序项目位置

时间:2013-06-18 04:41:49

标签: jquery jquery-ui

在JqueryUI可排序中,当用户拖动项目时,更改名为的事件。我在下面的例子中得到了这个。

jsFiddle code here

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 获取值(如果我知道拖动项的当前位置,那么只有我可以访问之前的项目)。如果验证成功,可以放置它,或者我必须显示一些消息。

2 个答案:

答案 0 :(得分:7)

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

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

<强> FIDDLE

答案 1 :(得分:0)

Jude Duran的回答不会经常发挥作用(在把我的头发拉出后发现了这一点)。第一个和最后一个项占位符根据鼠标位置移动。

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

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

相关问题