javascript - Sortable list + Finding dropped index - Stack Overflow

OK, I'm experimenting a bit with RubaXa's Sortable plugin. (Here's a great example page)

OK, I'm experimenting a bit with RubaXa's Sortable plugin. (Here's a great example page)

    var sort = new Sortable($('#items')[0], {
        animation: 150,

        onUpdate: function(evt/**Event*/){
            var item = evt.item;
            console.log(evt);
        }
    });

The Plugin works fine. The thing is how can I get the index at which the element has been dropped? (e.g. from index 2 of the list to index 0)

Demo: /

OK, I'm experimenting a bit with RubaXa's Sortable plugin. (Here's a great example page)

    var sort = new Sortable($('#items')[0], {
        animation: 150,

        onUpdate: function(evt/**Event*/){
            var item = evt.item;
            console.log(evt);
        }
    });

The Plugin works fine. The thing is how can I get the index at which the element has been dropped? (e.g. from index 2 of the list to index 0)

Demo: http://jsfiddle/j7fesLkp/1/

Share Improve this question edited Nov 12, 2014 at 7:50 Dr.Kameleon asked Nov 11, 2014 at 17:24 Dr.KameleonDr.Kameleon 22.8k21 gold badges124 silver badges237 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

The event that's passed to onSort has the fields you need: oldIndex and newIndex:

var sort = new Sortable(items, {
    onSort: function (evt) {
        console.log(evt.oldIndex + ' -> ' + evt.newIndex);
    }
});
<!-- Sortable -->
<script src="https://rawgit./RubaXa/Sortable/dev/Sortable.js"></script>

<ul id="items">
    <li data-id="1">item 1</li>
    <li data-id="2">item 2</li>
    <li data-id="3">item 3</li>
    <li data-id="4">item 4</li>
    <li data-id="5">item 5</li>
</ul>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745337303a4623164.html

相关推荐

  • javascript - Sortable list + Finding dropped index - Stack Overflow

    OK, I'm experimenting a bit with RubaXa's Sortable plugin. (Here's a great example page)

    4小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信