跟踪 jQuery 可排序中所有元素的位置
我试图弄清楚如何跟踪 jQuery 中的元素如何排序。 假设我的可排序中有四个元素(div),每个元素都有一个唯一的 id。我们将它们称为#100、#200、#300 和#400,它们首先按此顺序呈现。但是,当我将 #100 移动到 3 号位置时,我需要能够保存该 id 的新位置,同时还要确保其他位置也获得更新的位置值。
我说得有道理吗?基本上我想获取每个唯一 id 的位置,这样我就可以保存列表的顺序以供以后使用。
我无法找到可排序的所有元素的报告,仅针对已移动的元素。
请为我指出正确的方向? 谢谢
I'm trying to figure out how I can track how the elements in a jQuery sortable.
Let's say that I have four elements (divs) in my sortable, each have an unique id. Let's call them #100, #200, #300 and #400 and they're presented in that order to start with. But when I move #100 to let's say spot number 3, I need to be able to save the new position for this id, but also make sure that the other ones get updated position values aswell.
Am I making any sense? Basically I want to get the positions of each unique id, so I can save the order of the list for later use.
I haven't been able to find something that reports for all the elements in a sortable, just for the one that was moved..
Point me in the right direction?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 sortable 中提供的停止事件来跟踪项目。这是一个小例子;
这会记录可排序中所有项目的数组及其 id 和当前索引。
这里还有一个 jsfiddle 的工作示例: http://jsfiddle.net/beyondsanity/HgDZ9/
You can use the stop-event provided to you in sortable to keep track of the items. This is a small example;
This logs an array of all the items in the sortable with their id's and their current indexes.
Here is a working example on jsfiddle as well: http://jsfiddle.net/beyondsanity/HgDZ9/
根据文档 http://api.jqueryui.com/sortable/#method-toArray 我建议你使用内置方法
toArray
。例子:
According to the docs http://api.jqueryui.com/sortable/#method-toArray I suggest you to use built-in method
toArray
.Example:
jQuery UI 可排序位置 可能重复。
基本上,要获取可排序元素(或只是普通的旧父元素)中元素的位置,只需类似以下内容:
如果没有移动任何内容,则此代码将返回 2(
index()
从零开始计数),并在元素重新排列时更新。这是一个演示:http://jsfiddle.net/XB5Dh/。您可以单击某个元素来查看其位置,拖动元素时会显示新位置。
Possible duplicate of jQuery UI Sortable Position.
Basically, to get the position of an element within a sortable (or just a plain old parent elemen), just something like
This code will return 2 if nothing has been moved (
index()
starts counting at zero), and updates when the elements are re-arranged.Here's a demo: http://jsfiddle.net/XB5Dh/. You can click an element to see its position, and the new position is shown when you drag an element.