最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Sortable list + Finding dropped index - Stack Overflow

programmeradmin1浏览0评论

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>

发布评论

评论列表(0)

  1. 暂无评论