I am using ngForOf to list dates, each date is represented by id
property which is actually its index + 1. All the dates are present in form of object in an array. Also, each date is a component instance.
The thing with ngForOf is that whenever I delete a date from the list, the whole DOM refreshes and the ID's are assigned all over again.
<app-date *ngFor="let date of dates | callback: filterDates; index as i" (delete)="del(i)"></app-date>
The Problem
Now when I say "delete", I am not splicing/removing the date from the array, but setting the action
property of the date to "deleted"
. This is so that I don't have to make HTTP requests for every date to delete it from the database.
Once the action
is "deleted"
, the date does not show up in the DOM, but is still present in the array. So the next time DOM refreshes, it is going to reassign the IDs and now I am going to have duplicate ID's in the array. This has many consequences, the biggest one being that I cannot delete the dates with the same ID.
I also tried to change ID of the date on every delete event but that also messes up the array.
Important
I am aware of trackBy
, but I don't understand how I can apply it to solve this problem and don't see a problem on the internet that relates to this.
Thanks.