Can anyone tell me how I can write a function in accept condition and then how does it finds out that what to accept and what not to accept.
For example, I want to accept div a
and div b
in accept condition. How can I write I through a function?
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- How to fix IE ClearType + jQuery opacity problem i
- void before promise syntax
- jQuery add and remove delay
If you want the droppable to accept a selection of elements you can do something like this:
This droppable will accept elements with the class "foo" or an element with the id "bar"
I've figured out a solution that works based on the condition that no draggable should be placed in a droppable already occupied by another draggable:
According to the Jquery documentation on Selectors.
Thus,
in their example will only act as if something has been dropped on it if it has the class 'special'. It looks like it can accept any Jquery selector.
If I understand your question correctly, you want to conditionally accept the dropped element based on custom code. Aberon's answer is the typical case: you want to allow only certain draggable options to be dropped based on their class, and all others will revert. If that answers your question, then fine.
However, there is a case for having the revert animation happen conditionally based on something more complex than a class match. In my own project, I am using drag-drop to add users to a group. If the dropped user is already in the group, I want the user helper element to revert back. Otherwise, I go ahead with an AJAX action to insert them. This is no substitute for back-end checking, but it's nice visual feedback.
I have looked elsewhere for a simple answer to this. Note to JQuery maintainers: to me, the most straightforward way would be to add some property to the event object in the drop function, like this:
Sadly that doesn't work. Here's what does "work", though: set the draggable element to always revert, and then hide the helper if the condition is met. You can get a reference to the helper by looking for the only element on the page that has the class ".ui-draggable-dragging". Here's an example, replace "isDropOK()" with your own logic:
So to recap, every element will always revert unless you step in on the drop event and manually hide the helper. The revert animation will still happen, but your users won't see it. It's a little hack, but the end result seems to work all right.
This is my solution:
Instead of using a class as accept, you can just use a function like and return true if it matches your criteria