MatTree does not reflect the changes to the nodes if trackBy function is used. For example, I have several items with trackBy function pointing to their "id" property. If I provide a new array with completely same ids, but with changes to some of the other properties, change detection does not occur.
When used with trackBy function MatTree should trigger change detection on all nodes, just like ngFor with trackBy does.
No changes are detected therefore data is not updated in template.
https://stackblitz.com/edit/angular-material2-issue-z8ubtt
Latest versions of Angular, Material (check StackBlitz). Any browser.
Same issue was observed last year with MatTable.
use forEachIdentityChange method in differ to track identity changes like as ngFor do?
Still happening, any news?
@andrewseguin Can you take a look since it looks like most of the logic for this is reused between the Table and Tree
I think the behavior in your stackblitz is working as expected. You're telling the trackBy function to track each change based on the id value. The initial value and the next value both have and id === 1 so the Tree doesn't see any changes.
If you change your tracking function to either of these it works.
treeTracker = (index, person) => person;
treeTracker = (index, person) => person.id + person.name;
or if the id changes for the button action, the tree updates
ts
changeName() {
this.dataSubject.next([{id: 2, name: 'Kara', expandable: false, level: 0}]);
}
@schuchard , however, the way of how treeTracker works is different with the trackBy of *ngFor. *ngFor will update the item if the item can be identified as same item (but item properties may be changed) by TrackBy. The purpose of trackBy is only for performance by not destroying an item's dom if it's just has some property changes.
Most helpful comment
@schuchard , however, the way of how treeTracker works is different with the trackBy of *ngFor. *ngFor will update the item if the item can be identified as same item (but item properties may be changed) by TrackBy. The purpose of trackBy is only for performance by not destroying an item's dom if it's just has some property changes.