在 jQuery UI 中克隆可拖动对象时,如何将数据和事件传输到新元素?

发布于 2024-11-16 00:57:01 字数 749 浏览 1 评论 0原文

我有一个设置了 helper: 'clone' 的可拖动元素,但是当它克隆该元素时,data() 或事件都不会持久保留在新元素中。

我尝试了多种方法来重新附加 data(),但我似乎无法在同一语句中选择新元素以及旧元素。

例如,我可以在draggable stop() 事件中选择初始元素:

$blah.draggable({
    helper: 'clone',
    stop: function(ev, ui) {
        var oldData = $(ev.target).data('blah');
    }
});

并且我还可以在droppable drop() 事件中获取新元素:

$blah.droppable({
    drop : function(ev, ui) {
        var $newElement = ui.draggable;
    }
});

但是我可以找不到在同一事件中同时获得两者的方法。

我想做的是以某种方式传输数据,例如:

$newElement.data('blah', $oldElement.data('blah'));

或者以其他方式使数据持久化,就像使用 $blah.clone(true);

I have a draggable element with the helper: 'clone' set, but when it clones the element, none of the data() or events are persistent in the new element.

I've tried a number of ways to reattach the data(), but I can't seem to select the new element as well as the old element in the same statement.

For instance, I can select the initial element in the draggable stop() event:

$blah.draggable({
    helper: 'clone',
    stop: function(ev, ui) {
        var oldData = $(ev.target).data('blah');
    }
});

And I can also get the new element in the droppable drop() event:

$blah.droppable({
    drop : function(ev, ui) {
        var $newElement = ui.draggable;
    }
});

But I can't figure out a way to get both in the same event.

What I'd like to do is somehow transfer the data, e.g.:

$newElement.data('blah', $oldElement.data('blah'));

Or otherwise make the data persistent, like you can with $blah.clone(true);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

下雨或天晴 2024-11-23 00:57:01

要访问 drop 中原始元素的数据,您可以使用 ui.draggable.context。在下面的示例中,上下文将引用原始拖动的元素,并且您可以访问其所有内容。 Draggable 指的是被拖放的新元素。

$("#droppable").droppable({
    drop: function(ev, ui) {        
        console.log(ui);
        console.log(ui.draggable.context);
        console.log($(ui.draggable.context).data('pic'));
    }
});

To get access to the data of original element in drop you can use ui.draggable.context. In the example below context would refer to the original dragged element and you have access to all of its content. Draggable refers to the new element that is being dropped.

$("#droppable").droppable({
    drop: function(ev, ui) {        
        console.log(ui);
        console.log(ui.draggable.context);
        console.log($(ui.draggable.context).data('pic'));
    }
});
半衬遮猫 2024-11-23 00:57:01

我还没有对 droppable 进行过广泛的研究,但是你就不能做这样的事情吗?

$(".draggable").draggable({
    helper: 'clone'
});

$("#droppable").droppable({
    drop: function(ev, ui) {
        $(this).append(ui.draggable.clone(true));
    }
});

似乎可以工作,除非我遗漏了一些东西:

http://jsfiddle.net/hasrq/

I haven't worked too extensively with droppable, but couldn't you just do something like this?

$(".draggable").draggable({
    helper: 'clone'
});

$("#droppable").droppable({
    drop: function(ev, ui) {
        $(this).append(ui.draggable.clone(true));
    }
});

Seems to work unless there's something I'm missing:

http://jsfiddle.net/hasrq/

我的奇迹 2024-11-23 00:57:01

事实证明,问题是可排序的,而不是可拖动/可放置的(我后来附加了可排序的内容,但认为这不是问题的一部分,所以我将其排除在原始问题之外)。

我最终使用了上面@kingjiv的解决方案的组合,以及一个不是最好的黑客,但至少它似乎有效:

$blah.sortable({
    receive: function(ev, ui) {
        // setting a global variable here
        MyGlobals.cloneCache = ui.item.clone(true);
    },
    stop: function(ev, ui) {
        $(ui.item).replaceWith(MyGlobals.cloneCache);
    }
});

这个想法是你首先克隆 receive() 中的原始项目 事件,将其缓存在变量中,然后将该项目替换为 stop() 事件中的项目。

有点丑,但至少它可以工作。

Turns out the problem was sortable, not draggable / droppable (I was attaching sortable later on, but figured it wasn't part of the problem here so I left it out of the original question).

I ended up using sort of a combination of @kingjiv's solution above, along with a not-the-greatest hack but at least it seems to be working:

$blah.sortable({
    receive: function(ev, ui) {
        // setting a global variable here
        MyGlobals.cloneCache = ui.item.clone(true);
    },
    stop: function(ev, ui) {
        $(ui.item).replaceWith(MyGlobals.cloneCache);
    }
});

The idea is that you first clone the original item in the receive() event, cache this in a variable, and then replace the item with that in the stop() event.

Kind of ugly but at least it's working.

半步萧音过轻尘 2024-11-23 00:57:01

ui.item 指的是拖动的项目。当拖动的项目被克隆时,没有内置方法可以从 receive 函数访问目标项目。然而,有一个有点hacky的方法来做到这一点:

$blah.sortable({
    receive: function (ev, ui) {
        var $target = $(this).data().sortable.currentItem;
        var $source = $(ui.sender);
        // now you can copy data from source to target
        $target.data('data-item', $source.data('data-item')); 
    } 
});

ui.item refers to dragged item. When the dragged item is cloned, there is no built-in way to access the target item from receive function. However, there is a bit hacky way how to do it:

$blah.sortable({
    receive: function (ev, ui) {
        var $target = $(this).data().sortable.currentItem;
        var $source = $(ui.sender);
        // now you can copy data from source to target
        $target.data('data-item', $source.data('data-item')); 
    } 
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文