简体   繁体   English

使用jquery.ui.sortable更新列表

[英]update a list by using jquery.ui.sortable

I have a list which is numbered. 我有一个编号列表。
When sorting the items I need to update a number which correspond to the position in the list. 在对项目进行排序时,我需要更新一个数字,该数字与列表中的位置相对应。

Here is my code: 这是我的代码:

$(".sortable").sortable({
    stop: function(event, ui) {
        var prevPos = ui.item.find('.position').text();
        var newPos  = ui.item.index() + 1
        ui.item.find('.position').text(newPos);
        console.log(prevPos);
    }
});

My problem is how should I update the other items? 我的问题是我应该如何更新其他项目?
For example when I move Robert from the 3dt to the 1st? 例如,当我将Robert从3dt移到1d时?

you can see the demo at this link: http://jsfiddle.net/UAcC7/106/ 您可以通过以下链接查看演示: http : //jsfiddle.net/UAcC7/106/

How about something like this instead: 像这样的事情怎么样:

$(".sortable").sortable({
    stop: function(event, ui) {
        $('.sortable li').each(function(i) {
            $(this).find('span').text(++i);
        });
    }
});

You will be updating all items every time, even the ones that theoretically don't need it. 您将每次都更新所有项目,即使是理论上不需要的项目。 However, I think it will be faster anyway since you don't need to traverse the DOM and do index calculations etc. 但是,我认为无论如何它都会更快,因为您不需要遍历DOM并进行索引计算等。

http://jsfiddle.net/UAcC7/107/ http://jsfiddle.net/UAcC7/107/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM