bug(drag-drop): previousIndex is not correct if list contains invisible items
See original GitHub issueReproduction
https://stackblitz.com/edit/angular-rs1rtm
move “Go home” item into “Done” list
Steps to reproduce:
- Define two drag-drop lists
- Add both visible and invisible (display: none) items into lists
- Drag’n’Drop some items from one list into another
- Sometimes wrong (invisible) item is moved (depending on an item position) (on cdkDropListDropped
event.previousIndexof a selected item is not an actual index in the list of items but index of only visible items in this list)
Expected Behavior
Correct (visible) item which was selected should be moved into list (on cdkDropListDropped event.previousIndex must be an index of an item in a list regardless of visibility)
Actual Behavior
On cdkDropListDropped event.previousIndex of a selected item is not an actual index in the list of items but index of only visible items in this list
Environment
- Angular: 9.1.0
- CDK/Material: 9.1.0-9.2.0
- Browser(s): Chrome 80.0.3987.149 (but I think it reproducible in all browsers)
- Operating System: macOS (but I think it reproducible in all OS)
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:10 (4 by maintainers)
Top Results From Across the Web
Angular material drag-drop design not working properly
When a cdkDrag element is picked up, it will create a preview element visible while dragging. By default, this will be a clone...
Read more >Drag & Drop Angular CDK - Medium
We can fix this by adding the cdkDropList attribute to the divs containing our list of to-do items. Now, if we try to...
Read more >Drag and Drop | Angular Material
Element to which to attach the drop list functionality. ... Emits when the user has moved a new drag item into this container....
Read more >DevComponents Product Release Notes Version 7.3.0.0
Fixed: Fixed bug in DragDrop for AdvTree and AdvGrid when selection mode is extended - items moved into incorrect position under certain conditions....
Read more >An Introduction to R - The Comprehensive R Archive Network
This will be the working directory whenever you use R for this particular problem ... If array structures are present and no error...
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

I just noticed that it messes up the previousIndex when you add
display: noneto the placeholder of the item.In my opinion it should not be that way. I have a list from which I have disabled sorting and that serves only as an infinite source of draggable items (items are copied when dropped in other lists). I want to hide the placeholder in that source list, but not in other lists, and I want my placeholder to look a specific way, so I use
*cdkDragPlaceholder… But when I hide the placeholder in the source list, the drop event sets the previousIndex to 0, regardless of the dragged item’s original position, just because the placeholder is hidden.Worked on 9.0.1, but broken on 9.1.0
Faced the same issue.
Workaround:
Add
cdkDragDatato thecdkDragelement template. In the OP’s StackBlitz that is:Now you can look on the receiving
cdkDropList’scdkDropListDroppedhandler for the correct index: