jQuery UI,可排序,我可以确定从X位置的元素转到Y吗?

时间:2015-02-19 15:34:45

标签: jquery jquery-ui-sortable

我所拥有的只是这个例子:http://jqueryui.com/sortable/现在我想要一个“魔术”事件告诉我,例如我将item5移动到item2并告诉我“5移动到2”位置

1 个答案:

答案 0 :(得分:1)

不,我认为根据api没有“魔法”事件,但这是一种方法:

var startArray, stopArray;
$("#sortable").sortable({
    start: function(){ //save items in array before they are sorted
        startArray = $(this).sortable('toArray');
    },
    beforeStop: function(){ //save new sortorder of items
        stopArray = $(this).sortable('toArray');
    },
    update: function(){ //if the dom is changed, check the differences
        $.each(stopArray, function(index, value){
            if(startArray[index] !== value){ //if the values don't fit, they are changed
                console.log(value + ' is now on position ' + (index+1));
            }
        });

    }
});

<强> Demo

<强>参考

event-start

event-beforeStop

event-update

.each()