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

查看:142
本文介绍了jQuery可排序获取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);
        }
    });

我尝试的所有选项都指向被拖动的元素,但不是它被交换的元素with: 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.

此外,指向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 |

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

所以问题是如何在这里获得第二个元素?

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

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

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);
        }
    });

谢谢,

Dmitriy。

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可排序获取2个被交换的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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