jQuery:一个可排序列表,在另一个列表上显示结果
我正在使用 jQuery UI Sortable 来使列表可排序。我还有另一个不可排序的列表,我想在对可排序列表进行排序时更新该列表,以便它显示与可排序列表相同的顺序。
display_only 列表中的项目将包含输入字段,因此理想情况下,“display_only”中的项目也应该在 DOM 中移动,但这不是必需的。
我已经尝试了几乎所有我能想到的方法,比如分离元素并尝试按照新顺序重新插入它等等,但我真的无法弄清楚。任何能引导我走向正确方向的事情都很棒!
示例 HTML:
<ul id="sortable">
<li data-id="1">Item 1</li>
<li data-id="2">Item 2</li>
<li data-id="3">Item 3</li>
</ul>
<ul id="display_only">
<li data-id="1">Item 1 and som other content</li>
<li data-id="2">Item 2 and som other content</li>
<li data-id="3">Item 3 and som other content</li>
</ul>
I'm using jQuery UI Sortable to make a list sortable. I also have another list that is not sortable, that i want to update when i sort the sortable list, so that it displays the same order as the sortable list.
The items in the display_only list will contain input fields, so ideally the items in "display_only" should also move in the DOM, but not an requirement.
I have tried just about everything that i can come up with, like detaching the element and trying to reinsert it at it's new order and so on, but i can't really figure it out. Anything that would lead me in the right direction would be great!
Example HTML:
<ul id="sortable">
<li data-id="1">Item 1</li>
<li data-id="2">Item 2</li>
<li data-id="3">Item 3</li>
</ul>
<ul id="display_only">
<li data-id="1">Item 1 and som other content</li>
<li data-id="2">Item 2 and som other content</li>
<li data-id="3">Item 3 and som other content</li>
</ul>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这里没有真正需要使用数组。但是,您可以轻松地将定义的数组项集成到其中。
我确信将其写入函数并将父选择器作为参数传递更合适。
There's no real need to use arrays here. But, you can easily integrate defining array items among this.
I'm sure writing it into a function and passing the parent selectors as args is more suitable though.
使用 $.fn.clone() 方法克隆排序后的 ul 怎么样?
并在每次你排序时触发一个克隆,
我不确定确切的语法,你需要研究,但我认为这应该有效,
注意 jquery ui sortable 有一堆事件,你应该为你绑定正确的一个 < a href="http://jqueryui.it/demos/sortable#events" rel="nofollow">jquery ui 可排序事件 我怀疑“stop”、“beforeStop”或“change”将是正确的
另一个编辑和解决方案可能是:在第一个列表排序后,自动使用事件绑定对另一个列表进行排序,例如:
简单而优雅
what about using the $.fn.clone() method to clone the sorted ul
and triggring a clone everytime you sort with
notice I'm not sure about the exact syntax, that yours to research, but I think this should work
notice that jquery ui sortable has a bunch of events and you should bind the right one for you jquery ui sortable events I suspect 'stop', 'beforeStop' or 'change' would be the right one
another edit and solution might be: just sorting the other list after the first one is sorted, automaticaly with event binding like:
simple and elegent
经过一番调整后,我让它工作了。我不确定这是否是解决该问题的最佳方法,但它确实有效。停止时,我调用函数对“#display_ only”中的项目进行重新排序,循环遍历新顺序,并将其推入数组中。
之后,我循环遍历“#display_only”中的项目并以相同的顺序分离它们,然后将它们附加到我的列表中,如下所示:
After a bit if tweaking i got it working. I'm not sure if this is the best way to solve it, but it works. On stop i call my function to reorder the items in "#display_ only", that loops through the new order, and pushes that into an array.
After that i loop through my items in "#display_only" and detach them in the same order, and then just appends them to my list, like this: