jQuery:一个可排序列表,在另一个列表上显示结果

发布于 2024-12-13 18:20:07 字数 722 浏览 0 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

記柔刀 2024-12-20 18:20:07

这里没有真正需要使用数组。但是,您可以轻松地将定义的数组项集成到其中。

 $('#sortable').sortable({
        start: function(event, ui){
                iBefore = ui.item.index();
        },
        update: function(event, ui) {
                iAfter = ui.item.index();
                evictee = $('#display_only li:eq('+iAfter+')');
                evictor = $('#display_only li:eq('+iBefore+')');

                evictee.replaceWith(evictor);
                if(iBefore > iAfter)
                    evictor.after(evictee);
                else
                    evictor.before(evictee);
        }
 });

我确信将其写入函数并将父选择器作为参数传递更合适。

There's no real need to use arrays here. But, you can easily integrate defining array items among this.

 $('#sortable').sortable({
        start: function(event, ui){
                iBefore = ui.item.index();
        },
        update: function(event, ui) {
                iAfter = ui.item.index();
                evictee = $('#display_only li:eq('+iAfter+')');
                evictor = $('#display_only li:eq('+iBefore+')');

                evictee.replaceWith(evictor);
                if(iBefore > iAfter)
                    evictor.after(evictee);
                else
                    evictor.before(evictee);
        }
 });

I'm sure writing it into a function and passing the parent selectors as args is more suitable though.

孤凫 2024-12-20 18:20:07

使用 $.fn.clone() 方法克隆排序后的 ul 怎么样?
并在每次你排序时触发一个克隆,

$('#sortable').bind('sort', function(e,ui){
   $newlist = $(this).clone();

   $('#display_only').remove();   //removing the old list
   $newlist.attr('id','display_only'); // giving the right id for the cloned element
   $(this).append($newlist)

});

我不确定确切的语法,你需要研究,但我认为这应该有效,

注意 jquery ui sortable 有一堆事件,你应该为你绑定正确的一个 < a href="http://jqueryui.it/demos/sortable#events" rel="nofollow">jquery ui 可排序事件 我怀疑“stop”、“beforeStop”或“change”将是正确的

另一个编辑和解决方案可能是:在第一个列表排序后,自动使用事件绑定对另一个列表进行排序,例如:

$('#sortable').bind('sort', function(e,ui){
   $("#display_only).sortable()

});

简单而优雅

what about using the $.fn.clone() method to clone the sorted ul
and triggring a clone everytime you sort with

$('#sortable').bind('sort', function(e,ui){
   $newlist = $(this).clone();

   $('#display_only').remove();   //removing the old list
   $newlist.attr('id','display_only'); // giving the right id for the cloned element
   $(this).append($newlist)

});

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:

$('#sortable').bind('sort', function(e,ui){
   $("#display_only).sortable()

});

simple and elegent

南薇 2024-12-20 18:20:07

经过一番调整后,我让它工作了。我不确定这是否是解决该问题的最佳方法,但它确实有效。停止时,我调用函数对“#display_ only”中的项目进行重新排序,循环遍历新顺序,并将其推入数组中。

之后,我循环遍历“#display_only”中的项目并以相同的顺序分离它们,然后将它们附加到我的列表中,如下所示:

function reorder() {
    var newOrder = [];
    $('#sortable li').each(function(i) {
         newOrder.push($(this).attr('data-id'));
    });
    $('#display_only li').each(function(i) {
         var item = $("li[data-id="+newOrder[i]+"]").detach();
         $('#display_only').append(item);
    });
}

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:

function reorder() {
    var newOrder = [];
    $('#sortable li').each(function(i) {
         newOrder.push($(this).attr('data-id'));
    });
    $('#display_only li').each(function(i) {
         var item = $("li[data-id="+newOrder[i]+"]").detach();
         $('#display_only').append(item);
    });
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文