|
| 1 | +import {ChangeDetectionStrategy, Component} from '@angular/core'; |
| 2 | +import {MatTreeModule} from '@angular/material/tree'; |
| 3 | +import {MatIconModule} from '@angular/material/icon'; |
| 4 | +import {MatButtonModule} from '@angular/material/button'; |
| 5 | + |
| 6 | +/** |
| 7 | + * Food data with nested structure. |
| 8 | + * Each node has a name and an optional list of children. |
| 9 | + */ |
| 10 | +interface FoodNode { |
| 11 | + name: string; |
| 12 | + children?: FoodNode[]; |
| 13 | +} |
| 14 | + |
| 15 | +const TREE_DATA: FoodNode[] = [ |
| 16 | + { |
| 17 | + name: 'Fruit', |
| 18 | + children: [{name: 'Apple'}, {name: 'Banana'}, {name: 'Fruit loops'}], |
| 19 | + }, |
| 20 | + { |
| 21 | + name: 'Vegetables', |
| 22 | + children: [ |
| 23 | + { |
| 24 | + name: 'Green', |
| 25 | + children: [{name: 'Broccoli'}, {name: 'Brussels sprouts'}], |
| 26 | + }, |
| 27 | + { |
| 28 | + name: 'Orange', |
| 29 | + children: [{name: 'Pumpkins'}, {name: 'Carrots'}], |
| 30 | + }, |
| 31 | + ], |
| 32 | + }, |
| 33 | +]; |
| 34 | + |
| 35 | +/** |
| 36 | + * @title Tree with nested nodes (childrenAccessor) |
| 37 | + */ |
| 38 | +@Component({ |
| 39 | + selector: 'tree-nested-child-accessor-overview-example', |
| 40 | + templateUrl: 'tree-nested-child-accessor-overview-example.html', |
| 41 | + styleUrl: 'tree-nested-child-accessor-overview-example.css', |
| 42 | + standalone: true, |
| 43 | + imports: [MatTreeModule, MatButtonModule, MatIconModule], |
| 44 | + changeDetection: ChangeDetectionStrategy.OnPush, |
| 45 | +}) |
| 46 | +export class TreeNestedChildAccessorOverviewExample { |
| 47 | + childrenAccessor = (node: FoodNode) => node.children ?? []; |
| 48 | + |
| 49 | + dataSource = TREE_DATA; |
| 50 | + |
| 51 | + hasChild = (_: number, node: FoodNode) => !!node.children && node.children.length > 0; |
| 52 | +} |
0 commit comments