在 jQuery UI 中克隆可拖动对象时,如何将数据和事件传输到新元素?
我有一个设置了 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
要访问 drop 中原始元素的数据,您可以使用 ui.draggable.context。在下面的示例中,上下文将引用原始拖动的元素,并且您可以访问其所有内容。 Draggable 指的是被拖放的新元素。
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 进行过广泛的研究,但是你就不能做这样的事情吗?
似乎可以工作,除非我遗漏了一些东西:
http://jsfiddle.net/hasrq/
I haven't worked too extensively with droppable, but couldn't you just do something like this?
Seems to work unless there's something I'm missing:
http://jsfiddle.net/hasrq/
事实证明,问题是可排序的,而不是可拖动/可放置的(我后来附加了可排序的内容,但认为这不是问题的一部分,所以我将其排除在原始问题之外)。
我最终使用了上面@kingjiv的解决方案的组合,以及一个不是最好的黑客,但至少它似乎有效:
这个想法是你首先克隆
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:
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 thestop()
event.Kind of ugly but at least it's working.
ui.item
指的是拖动的项目。当拖动的项目被克隆时,没有内置方法可以从receive
函数访问目标项目。然而,有一个有点hacky的方法来做到这一点:ui.item
refers to dragged item. When the dragged item is cloned, there is no built-in way to access the target item fromreceive
function. However, there is a bit hacky way how to do it: