开发者

jquery revert dragging if droppable already contains that element

开发者 https://www.devze.com 2023-04-12 21:55 出处:网络
I am facing the dragging prob开发者_StackOverflow中文版lem. Here\'s the fiddle. http://jsfiddle.net/dJyUQ/16/

I am facing the dragging prob开发者_StackOverflow中文版lem. Here's the fiddle. http://jsfiddle.net/dJyUQ/16/

There are 4 boxes (A, B, C, D) and all are droppables. ".item" div is draggable and can be dropped on any of the 4 droppables.

I want just one copy of item to be dropped on one droppable. So that when I attempt to again drop copy of item on the same droppable it wouldn't allow me. Currently I have inserted an alert. But I want to cancel or revert dragging.

Please help me as I think I am missing something. I searched jquery ui dragging and droppable plugin but could not find anything.


Change your drop code to be this:

drop: function(event, ui){
    if($(this).children(".itemcopy").size() > 0) {
        alert("cancel");    //Instead want to cancel dragging of item
        return false;
    }
    $(this).append("<div class='itemcopy'>"+ui.draggable.html()+"</div>");        
}

http://jsfiddle.net/dJyUQ/18/


You can use this trick from this blog article: using revert: function(socketObj)

Here is a working example: http://jsfiddle.net/dJyUQ/16/

[EDIT] Richard's answer is far better. I'll leave my answer for the use of a function within the revert method (I found this was a good idea though, have a look at the article).


change the drop code to the following

drop: function(event, ui){
               if(!($(this).children(".itemcopy").size() == 1)) {
                 $(this).append("<div class='itemcopy'>"+ui.draggable.html()+"</div>");   
        }
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号