开发者

get the start position of an item using the jquery ui sortable plugin

开发者 https://www.devze.com 2022-12-26 05:50 出处:网络
I am using the jQuery UI sortable plugin and I am trying to get 2 alerts I want the staring position of the element and the finished position of the element.

I am using the jQuery UI sortable plugin and I am trying to get 2 alerts

I want the staring position of the element and the finished position of the element.

$(function() {
    $("#filterlist ul").sortable({ opacity: 0.6, cursor: 'move', update: function(event, ui) {
            alert(ui.item.prevAll().length + 1);
        }
    });
});

I can get the position of the item after it has been dragged by using:-

ui.item.prevAll().length + 1

What do I use to get the position 开发者_如何转开发it started from?


$(function() {

    $("#sortable").sortable({
       start: function(event, ui) { console.log('before @ '+ ui.item.index()) },
       update: function(event, ui) { console.log('now @ '+ ui.item.index()) }
    });

});​

try this demo and watch at the console...


Use the start event and "cache" the starting position

var start;
$(function() {
    $("#filterlist ul").sortable({
        opacity: 0.6,
        cursor: 'move',
        start: function(event, ui) {
            start = ui.item.prevAll().length + 1;
        },
        update: function(event, ui) {
            alert(start + " -> " + (ui.item.prevAll().length + 1));
        }
    });
})
0

精彩评论

暂无评论...
验证码 换一张
取 消