ng-sortable icon indicating copy to clipboard operation
ng-sortable copied to clipboard

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

Open isaaclyman opened this issue 9 years ago • 23 comments

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.

isaaclyman avatar Sep 08 '15 19:09 isaaclyman

:+1: had the same problem

sp90 avatar Sep 18 '15 09:09 sp90

+1

platdesign avatar Sep 21 '15 18:09 platdesign

+1

georgediaz88 avatar Sep 25 '15 20:09 georgediaz88

I have this problem

diogomachado avatar Sep 30 '15 12:09 diogomachado

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.

diogomachado avatar Oct 04 '15 19:10 diogomachado

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.

jljouannic avatar Oct 23 '15 10:10 jljouannic

This solution seems cleaner than other ones presented:

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

brianfeister avatar Oct 31 '15 22:10 brianfeister

+1 for a solution for this

developerant avatar Nov 28 '15 03:11 developerant

+1

sebastianteres avatar Dec 04 '15 00:12 sebastianteres

+1

jhendley25 avatar Dec 14 '15 15:12 jhendley25

+1

wtser avatar Jan 04 '16 06:01 wtser

+1

haschu avatar Jan 12 '16 11:01 haschu

+1

pdxwebdev avatar Jan 19 '16 05:01 pdxwebdev

+1

joevanwanzeele avatar Feb 08 '16 18:02 joevanwanzeele

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

Elegant-Bird avatar Feb 10 '16 19:02 Elegant-Bird

+1

bestmazzo avatar Feb 17 '16 14:02 bestmazzo

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.

davidsandoz avatar Feb 24 '16 16:02 davidsandoz

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.

sonmays avatar May 16 '16 23:05 sonmays

+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).

rdeutsch avatar Jul 06 '16 00:07 rdeutsch

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.

connormlewis avatar Aug 14 '16 04:08 connormlewis

+1

btk avatar Aug 24 '16 13:08 btk

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)

daftspaniel avatar Aug 02 '17 08:08 daftspaniel

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.

DiegoAugustusCunha avatar Aug 13 '20 13:08 DiegoAugustusCunha