a5hik / ng-sortable

AngularJS Library for Drag and Drop, supports Sortable and Draggable. Supports Touch devices.
http://a5hik.github.io/ng-sortable/
MIT License
1.15k stars 314 forks source link

`track by $index` in ng-repeat causes list items to disappear #221

Open isaaclyman opened 9 years ago

isaaclyman commented 9 years ago

If you add track by $index to the ng-repeat expression of the node with the as-sortable-item directive on it, then the drag-and-drop behavior breaks. When dragging any item to its own place in the list, it disappears, though sometimes dragging another item will cause it to reappear in place of the first item.

http://plnkr.co/edit/uCtpVyS3LmveHv9zrt14?p=preview

The only thing that has been changed is adding track by $index to both ng-repeat statements.

To see the breaking behavior, drag an item from position 2 to position 3 in the list, then click and drag the item in position 2 but don't change its place -- drop it back where it was.

This may be a problem for anyone trying to optimize an AngularJS application, since one of the best ways to speed up ng-repeat rendering is to add a track by statement.

sp90 commented 9 years ago

:+1: had the same problem

platdesign commented 9 years ago

+1

georgediaz88 commented 8 years ago

+1

diogomachado commented 8 years ago

I have this problem

diogomachado commented 8 years ago

My solution for now: https://gist.github.com/diogomachado/575c2fe477d0e53429bc

I see in the core code the project and he use $scope associate, i believe this is blocking the directive when used track $index.

jljouannic commented 8 years ago

The problem seems to have been introduced in version 1.3.1. I have other problems with version 1.3.0, but not this one.

brianfeister commented 8 years ago

This solution seems cleaner than other ones presented:

https://github.com/a5hik/ng-sortable/issues/128

developerant commented 8 years ago

+1 for a solution for this

sebastianteres commented 8 years ago

+1

jhendley25 commented 8 years ago

+1

wtser commented 8 years ago

+1

haschu commented 8 years ago

+1

pdxwebdev commented 8 years ago

+1

joevanwanzeele commented 8 years ago

+1

Elegant-Bird commented 8 years ago

Just to be clear, it's not only happening with an explicit "track by $index." The issue also occurs when adding order By: '-property' or making references to the implicit $index in general

bestmazzo commented 8 years ago

+1

davidsandoz commented 8 years ago

For the ones using the orderBy filter, a workaround is to avoid using it and rather sort the array beforehand. Then no more weird reordering or items disappearing.

sonmays commented 8 years ago

After poking around I found that the asSortableItem takes an optional ngModel, which will update the modelValue, so when comparing values in the apply function it has the right value. If you add ngModel to the same line as asSortableItem it should fix this issue.

rdeutsch commented 8 years ago

+1 for fixing this

However, the workaround above to include ngModel (along with ng-repeat) for the asSortableItem element seems to work for me. (in a case which uses orderBy filter for the ng-repeat).

connormlewis commented 8 years ago

This unfortunately is a deciding factor for me to not use this directive because I have no choice but to use track by $index to avoid angular's duplicates error inside the ng-repeat. I've seen several times the maintainer say that this is "a documented issue", but never seen any comment on if we should see a fix coming or not.

btk commented 8 years ago

+1

daftspaniel commented 7 years ago

Hit this issue this morning in a drag and drop situation. Some items were not being displayed despite the data being intact.

Avoiding use of $index seemed to solve it, I used track by (myarray.length + item.id*100)

DiegoAugustusCunha commented 4 years ago

Hit this issue this morning in a drag and drop situation. Some items were not being displayed despite the data being intact.

Avoiding use of $index seemed to solve it, I used track by (myarray.length + item.id*100)

I had the same problem using the version of bower 1.3.8, your solution solved perfectly.