jQueryUI与AngularJS结合使用可排序的起始位置/结束位置?

时间:2014-10-24 10:02:46

标签: jquery angularjs jquery-ui angular-ui jquery-ui-sortable

根据这个问题(和答案),我应该能够以多种方式获得原始的起始位置和结束位置:

jQuery UI Sortable, how to determine current location and new location in update event?

$('#sortable').sortable({
    start: function(e, ui) {
        // creates a temporary attribute on the element with the old index
        $(this).attr('data-previndex', ui.item.index());
    },
    update: function(e, ui) {
        // gets the new and old index then removes the temporary attribute
        var newIndex = ui.item.index();
        var oldIndex = $(this).attr('data-previndex');
        $(this).removeAttr('data-previndex');
    }
});

或更新事件中的所有内容(首选):

$('#sortable').sortable({    
        update: function(e, ui) {
            // ui.item.sortable is the model but it is not updated until after update
            var oldIndex = ui.item.sortable.index;

            // new Index because the ui.item is the node and the visual element has been reordered
            var newIndex = ui.item.index();
        }    
});

但是,正如一些关于这个问题的评论中所提到的,这对我来说并不适用。我或许应该提到我将可排序与AngularJS结合使用,特别是the wrapper provided by AngularUI。对于第一个示例,我将$(this)替换为$(e.target)

有关为什么这不起作用以及如何获得所需结果的任何线索?

1 个答案:

答案 0 :(得分:1)

好的,所以我找到了解决方案:

$('#sortable').sortable({ 
    update: function(e, ui) {
        oldPos = ui.item.sortable.index;
        newPos = ui.item.sortable.dropindex;
    }
});

这是基于Sortable的AngularUI包装提供的extra properties

相关问题