jQuery sortable 获取 2 个被交换的元素 [英] jQuery sortable obtain 2 elements being swapped

查看:33
本文介绍了jQuery sortable 获取 2 个被交换的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道如何使用可排序的 jQuery UI 获取目标元素.

I cannot find out how to obtain destination element with jQuery UI sortable.

    $("#pages").sortable({
        opacity: 0.6,
        update: function(event, ui) {
            var first = ui.item; // First element to swap
            var second = ???? // Second element to swap
            swapOnServer(first, second);
        }
    });

我尝试过的所有选项都指向被拖动的元素,但不是与它交换的元素:ui.item[0], event.srcElement, event.toElement.

All the options I've tried point to the element being dragged, but not the one it is swapped with: ui.item[0], event.srcElement, event.toElement.

此外,this 指向 LIST (OL) 元素.

Additionally, this points to the LIST (OL) element.

第二我的意思是:

原订单是:

|0 |1 |2 |3 |

| 0 | 1 | 2 | 3 |

我们拖动元素 1 并将其放在位置 3 上.最后会是:

We drag element 1 and drop it in position 3. Which will end up with:

|0 |3 |2 |1 |

| 0 | 3 | 2 | 1 |

所以第一个元素是 1,第二个是 3(错误!见下文).

So the first element is 1 and the second is 3 (WRONG! See below).

更新:我意识到我弄错了.在这种情况下,新的顺序是.

UPDATE: I have realised that I got it wrong. The new order in this case will be.

|0 |2 |3 |1 |

| 0 | 2 | 3 | 1 |

因此,我的问题没有意义.谢谢大家的帮助.我会标记投票并标记答案.

那么问题是如何获取这里的第二个元素?

So the question is how to obtain the second element here?

当前的解决方法(因为在 sortable 中没有 swapping 这样的术语)如下.它使用带有订单的临时数组.

THE CURRENT WORKAROUND (as there is no term as swapping in sortable) is below. It uses temporary array with orders.

    var prevPagesOrder = [];
    $("#pages").sortable({
        start: function(event, ui) {
            prevPagesOrder = $(this).sortable('toArray');
        },
        update: function(event, ui) {
            var currentOrder = $(this).sortable('toArray');
            var first = ui.item[0].id;
            var second = currentOrder[prevPagesOrder.indexOf(first)];
            swapOnServer(first, second);
        }
    });

谢谢,
德米特里.

Thanks,
Dmitriy.

推荐答案

本身并没有真正的第二个"项目.你有一个项目,你只是把它放在另一个位置.它周围的物品会相应地调整它们的位置.如果你想获得所有项目的数组,你可以使用 toArray 方法.

There's not really a "second" item per se. You have an item, and you are simply placing it in another location. The items around it adjust their positions accordingly. If you want to get an array of all the items, you can use the toArray method.

这篇关于jQuery sortable 获取 2 个被交换的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆