question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

When onAdd function is triggered, new item dragged is already into the list

See original GitHub issue

I 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:closed
  • Created 9 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
romanrdgzcommented, Jan 16, 2015

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?

0reactions
omgggacommented, Aug 25, 2015

I have found 3 ways to prevent dropping item to the other list already contains that item.

  • on start event get all collections, look for similar element and setting custom class for list element to prevent drop like @dandv said.
  • on add event look for all elements in event.to list, and if there are more than 1 similar element, remove it
  • on any start / end / update event look into result of Sortable.toArray() and then filter elements by uniq id

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found