简体   繁体   English

处理jQuery上的可排序列表

[英]Dealing with a sortable list on jQuery

I am using the "sortable" script ( http://farhadi.ir/projects/html5sortable/ ) to have a sortable list of items on my web application. 我使用“可排序”脚本( http://farhadi.ir/projects/html5sortable/ )在我的Web应用程序上有一个可排序的项目列表。 The sorting part works great. 分拣部分效果很好。 But now, I need to be able to ping back to my server to set the order when things change. 但现在,我需要能够ping回我的服务器,以便在事情发生变化时设置顺序。

The way to capture that event is: 捕获该事件的方法是:

$('.sortable').sortable().bind('sortupdate', function() {
    //Triggered when the user stopped sorting and the DOM position has     changed.
});

How do I understand the new position in order to inform my server? 我如何理解新职位以通知我的服务器? Since the lists are small, I thought of two approaches: 由于列表很小,我想到了两种方法:

1 - Understand in which position the item was dragged, look at the position right below and send some sort of "insert" update function to my server to insert the new item in that position. 1 - 了解项目被拖动的位置,查看右下方的位置,并向我的服务器发送某种“插入”更新功能,以便将新项目插入该位置。

2 - Just go through all the items on the list, capture their IDs in order and send the new ID list to the server and have the server re-build the index based on that list. 2 - 只需遍历列表中的所有项目,按顺序捕获其ID并将新ID列表发送到服务器,并让服务器根据该列表重新构建索引。

I can handle the ruby code but I am a disaster with JS/Jquery. 我可以处理ruby代码,但我是JS / Jquery的灾难。

So, if I go with option 1 - how do I identify the position of the new item and how do I identify which is the item right below it? 因此,如果我选择选项1 - 如何识别新项目的位置以及如何识别其下方的项目?

If I go with option 2 - how do I iterate on all li items in the exact order they are positioned (their DOM order) so that I can build this list and send it back to the server? 如果我选择选项2 - 如何按照它们所处的确切顺序(它们的DOM顺序)迭代所有li项目,以便我可以构建此列表并将其发送回服务器?

Read the ids of the lis when the update is fired 在触发更新时读取lis的id

var ids = $(".sortable").find("li").map( function(txt, elem){
   return elem.id
} ).get();
console.log(ids);

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

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