When onAdd function is triggered, new item dragged is already into the list
See original GitHub issueI am iterating all list elements in order to determine if the list already had the item I just droped into. I’m using Jquery:
$("#li").each(function(){ }
But the item just dropped is already there. Is there other event triggered before onAdd which would allow me to iterate list items before inserting the new item?
Issue Analytics
- State:
- Created 9 years ago
- Comments:7 (2 by maintainers)
Top Results From Across the Web
Vue Draggable add event picking up incorrect item
Now if I drag "John" or "Joao" to the 2nd list, the event is not correctly picking up the item that was dragged...
Read more >Define options to pass to a sortable object. - GitHub Pages
Use this function to define the options for sortable_js and rank_list, ... JS function called when an item is moved in a list...
Read more >Sortable - Best of JS
A minimalist JavaScript library for reorderable drag-and-drop lists on modern ... Element is dropped into the list from another list onAdd: function ......
Read more >EditableList Widget - Node-RED
A callback function that gets called when a new item is being added to the list. ... If it was triggered by clicking...
Read more >Drag operations - Web APIs - MDN Web Docs - Mozilla
When a user begins to drag, the dragstart event is fired. In this example the dragstart listener is added to the draggable element...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Thanks for your answer.
Maybe my oppinion is worth 0, since I am a JS newbie, and this might be something obvious to everybody else. But I think this should really be explained at the documentation.
As it is currently explained, start event is triggered when I drag an element from list 1 (but haven’t dropped it yet over list 2). I have tested, and it does as suggested, so it doesn’t serve my purposes, because i need an event once the item has been dropped into list 2, but has not been included as an element of list 2 yet.
Then, sort event is explained as an event triggered on insert, update, or delete. I had tested this event, and I had seen that indeed it was triggered before the add event. But since the event is also triggered on update (which I guess is just a reordering of existing elements) and delete, logic would get mixed and become harder to maintain, so it does not look as the better choice. Moreover, when onSort is triggered, the dropped item has been inserted as an element as happened with onAdd, so again it does not solve my problem.
About add, as I have commented in my question, it is a late event for me, since the element has already been inserted to the list, and I am not able to iterate old list elements (unless I use HTML5 custom data to insert a flag into each item, but I think there must be a better and cleaner way to achieve this).
What I am trying to achieve, is to detect if the dropped element was already in the list, so I can avoid duplicates and increment the badge indicating the number of that element into the list. I do think this is an important feature, and current library status doesn’t allow an obvious way of getting this information. Please consider to add a feature for badge increment (for example using duplicates: false at Sortable list constructor) or at least improve documentation on events.
Therefore, is it possible to add a new event being triggered once an item is dropped into the list, but before that item becomes part of the list?
I have found 3 ways to prevent dropping item to the other list already contains that item.
First way has a lower performance in working with large collections. Second and third have animation of drop and instant remove, so it looks like a bug.
Anyway, Sortable can provide you all you need on start and move events, but there ill be performance issues on checking duplicate elements.