jquery 可删除 ->避免同一物体多次掉落
我有一个包含不同可拖动元素的容器,并且有一些“目标”div 的列表,用户可以在其中放置可拖动元素。
例子: 想象一下,您有一个“标签”列表(房屋、计算机、汽车等)和一些作为目标文档的列表(所有文档都是 div )
用于使“标签”可拖动的代码:
$('#taglist').find('div').draggable(
{helper: "clone"});
用于使文档“可放置”的代码:
$('#doclist').droppable({
drop: function( event, ui )
{tag=ui.draggable;
tag.clone().appendTo( this );
} });
直到现在,这效果很好。 问题是,现在您可以将相同的标签多次分配给相同的文档。 示例:文档1可以获得标签“House”5次,标签“Computer”3次。
我的目标是,每个文档只能拥有每个标签一次。
我不知道,如何解决这个问题。现在,我认为有以下方法:
1.) 通过遍历 DOM $(this).find... 来扩展“drop”函数,以查看是否存在具有相同 id 的元素 - 在这种情况下,不要再次克隆和追加。可能这需要大量的性能。
2.) 使用可拖动小部件的“接受”功能。但我不知道在这种情况下如何使用它。
感谢您提供任何帮助。
I have a container with different draggable -elements and there is a list of some "target" divs, where the user can drop the draggable elements.
Example:
Imagine, you have a list of "tags" (House,Computer,Car,..) and a list of some documents as target (all documents are part of the div <div id="doclist">
). So the target is to assign the "tags" to the document using drag & drop. By the way, every tag-Div has an unique id (<div id="e34a568b2">
)
Code for making the "tags" draggable:
$('#taglist').find('div').draggable(
{helper: "clone"});
Code for making the documents "droppable":
$('#doclist').droppable({
drop: function( event, ui )
{tag=ui.draggable;
tag.clone().appendTo( this );
} });
Until now, this works well.
The Problem is, that right now you can assign the same tag multiple times to same documents.
Example: document 1 can get tag "House" 5 times, Tag "Computer" 3 times.
My target is, that every document can have every tag only one time.
I don't know, how to solve this problem. Right now, i thnik there are to ways:
1.) expand the "drop" function by walking trough the DOM $(this).find... to see, if there is an element with the same id - in this case, don't clone&append again. Probably this needs a lot of performance.
2.) use the "accept" feature of the draggable widget. But i don't know how to use this at this situation.
Thank you for any kind of help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,您应该确保页面中永远不会有两个具有相同 id 的元素。因此,在删除时,您希望以某种方式更改 id,例如:
接下来,您确实可以使用
accept
并检查 DOM。别担心,我认为这不会太占用资源。像这样的东西:First, you should make sure to never have two elements with the same id in the page. So when dropping, you want to change the id in some manner, e.g.:
Next, indeed you could use
accept
and checking the DOM. Don't worry, I don't think it will be too resource intensive. Something like:好吧,所以我想稍微改进一下该代码,并删除一条错误消息,说明为什么该项目不被接受。但它添加了项目打印错误然后它不接受然后没有打印为什么会发生?
有 2 个限制;
然后检查所有项目的计数是否大于 4 或等于?
接受:函数(可拖动){
if($(this).find("#copy-" + Draggable.attr("菜单项ID")).length == 0)
{
}
okey so i wanted to improve that code a little bit and drop an error msg why the item is not accepted. but it adds the item prints error then it doesnt accept then no prints why does happen?
There are 2 limitations;
then check to see if count of all the item more than 4 or equal?
accept: function(draggable) {
if($(this).find("#copy-" + draggable.attr("menu-item-id")).length == 0)
{
}