angular / angular/components

Use of custom TrackByFunction in MatTree does not preserve expansion state when nodes change

Aperta
#15,872 8 commenti 17 reazioni 0 assegnatari Vedi su GitHub
area: material/tree G P2
Lingua principale
TypeScript
Stelle
25k
Fork
6.8k
Merge medio
1g 8h
PR unite (30g)
91

Descrizione

#### What is the expected behavior?

Use of trackBy should also ensure that the expansion state does not get lost when nodes are updated.

#### What is the current behavior?

Use of trackBy does not ensure that the expansion state does not get lost when nodes are updated. This is presumably since TreeControl uses an expansionModel which is a SelectionModel where the SelectionModel has no awareness of the TrackByFunction.

#### What are the steps to reproduce?

Relying upon the TreeControl expansionModel:
https://stackblitz.com/edit/angular-5wpr9o-kjnmsn?file=app%2Ftree-flat-overview-example.ts

#### Which versions of Angular, Material, OS, TypeScript, browsers are affected?

all

#### Is there anything else we should know?

Trying to store expansion state in the nodes themselves as a workaround doesn't work (unless you transfer the state):
https://stackblitz.com/edit/angular-5wpr9o-ahptjs?file=app%2Ftree-flat-overview-example.ts

Having a custom expansionModel that is aware of the trackBy state works to preserve state, since the TreeControl's expansion-related methods apparently aren't actually used internally by the tree component:
https://stackblitz.com/edit/angular-5wpr9o-pbmk1g?file=app%2Ftree-flat-overview-example.ts

Guida per i contributori

Apri la guida per i contributori

Direzione di ricerca

Inizia dalle riproduzioni StackBlitz collegate, in particolare app/tree-flat-overview-example.ts, e analizza come interagiscono MatTree, TreeControl, TrackByFunction e expansionModel. Riproduci la perdita dello stato di espansione quando i nodi vengono aggiornati con un trackBy personalizzato. Il lavoro è completato quando lo stato di espansione rimane preservato dopo questi aggiornamenti.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
angular, typescript
Ambito
frontend
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
38/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.