help(Tree): TreeControl selection model doesn't work for immutable data
- Vorherrschende Sprache
- TypeScript
- Sterne
- 25k
- Forks
- 6.8k
- Ø Merge
- 1 T. 8 Std.
- Gemergte PRs (30 T.)
- 91
Beschreibung
Please see this - https://stackoverflow.com/q/65412659/2896495.
I needed a dynamically loading nodes in the angular material tree, so I created my own `GeoDataSource` which internally uses **_@ngrx/component-store_** to handle all the interaction between data source, TreeControl and the tree itself.
Now, the problem is the tree doesn't keep its expansion state because the `abstract class BaseTreeControl` uses `SelectionModel` class and this in turn uses `Set` to keep track of items that are selected. So if node is expanded it just does this:
```
this._selection.add(value); // Set.add(value)
```
When using immutable data (from the component store) those items are always new.
I tried to use `trackBy` optional function:
```
this.treeControl = new FlatTreeControl(
(node) => node.nodeLevel,
(node) => node.expandable, {
trackBy: (node) => node.groupPath // *** groupPath is string (it's an ID) ***
}
);
```
and for this I had to change `FlatTreeControl` to `FlatTreeControl`, otherwise it doesn't compile. But then I got the following error in the **_template_**:
> Type 'FlatTreeControl' is not assignable to type 'TreeControl'
Here's the template:
```
```
Now what? How do I make all this work with immutable data from the store?
Beitragsleitfaden
Rechercherichtung
Beginnen Sie mit dem Lesen von BaseTreeControl und SelectionModel und vergleichen Sie anschließend das trackBy-Generic von FlatTreeControl mit dem TreeControl-Typ, den das mat-tree-Template erwartet. Reproduzieren Sie den Fall mit unveränderlichen Daten aus dem Issue und überprüfen Sie, dass der Expansionsstatus über das Ersetzen von Knoten hinweg erhalten bleibt, während die Template-Typen kompatibel bleiben.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- angular, typescript
- Bereich
- frontend
- Issue-Typ
- Bug
- Schwierigkeit
- 5/5
- Geschätzter Aufwand
- Über eine Woche
- Aktivitätsstatus
- Veraltet
- Klarheit
- Muss geklärt werden
- Anfängerfreundlichkeit
- 25/100