My current setup uses ngForOf to display dates, with each date having an id
property which is basically its index + 1. These dates are stored as objects in an array and each date is represented by a component instance.
The issue I am facing with ngForOf is that whenever I remove a date from the list, the entire DOM refreshes and new IDs are reassigned.
<app-date *ngFor="let date of dates | callback: filterDates; index as i" (delete)="del(i)"></app-date>
The Dilemma
When I use the "delete" function, I do not actually splice/remove the date from the array; instead, I set the action
property of the date to "deleted"
. This saves me from making HTTP requests to delete each date from the backend database.
Once the action
is set to "deleted"
, the date disappears from the DOM but remains in the array. Therefore, during the next DOM refresh, new IDs are assigned leading to duplicate IDs in the array. This causes issues such as the inability to delete dates with the same ID.
I did attempt to update the ID of the date on every deletion event, but this also caused problems within the array.
Key Point
While I have some knowledge about trackBy
, I am unsure how to implement it to resolve this particular issue. My search for similar problems online has been unsuccessful so far.
Thank you.