|
1 | | -<span class="hljs-keyword">import</span> {<span class="hljs-title class_">FlatTreeControl</span>} <span class="hljs-keyword">from</span> <span class="hljs-string">'@angular/cdk/tree'</span>; |
2 | 1 | <span class="hljs-keyword">import</span> {<span class="hljs-title class_">ChangeDetectionStrategy</span>, <span class="hljs-title class_">Component</span>} <span class="hljs-keyword">from</span> <span class="hljs-string">'@angular/core'</span>; |
3 | | -<span class="hljs-keyword">import</span> {<span class="hljs-title class_">MatTreeFlatDataSource</span>, <span class="hljs-title class_">MatTreeFlattener</span>, <span class="hljs-title class_">MatTreeModule</span>} <span class="hljs-keyword">from</span> <span class="hljs-string">'@angular/material/tree'</span>; |
| 2 | +<span class="hljs-keyword">import</span> {<span class="hljs-title class_">MatTreeModule</span>} <span class="hljs-keyword">from</span> <span class="hljs-string">'@angular/material/tree'</span>; |
4 | 3 | <span class="hljs-keyword">import</span> {<span class="hljs-title class_">MatIconModule</span>} <span class="hljs-keyword">from</span> <span class="hljs-string">'@angular/material/icon'</span>; |
5 | 4 | <span class="hljs-keyword">import</span> {<span class="hljs-title class_">MatButtonModule</span>} <span class="hljs-keyword">from</span> <span class="hljs-string">'@angular/material/button'</span>; |
6 | 5 |
|
|
13 | 12 | <span class="hljs-attr">children</span>?: <span class="hljs-title class_">FoodNode</span>[]; |
14 | 13 | } |
15 | 14 |
|
16 | | -<span class="hljs-comment">/** Flat node with expandable and level information */</span> |
17 | | -<span class="hljs-keyword">interface</span> <span class="hljs-title class_">ExampleFlatNode</span> { |
18 | | - <span class="hljs-attr">expandable</span>: <span class="hljs-built_in">boolean</span>; |
19 | | - <span class="hljs-attr">name</span>: <span class="hljs-built_in">string</span>; |
20 | | - <span class="hljs-attr">level</span>: <span class="hljs-built_in">number</span>; |
21 | | -} |
22 | | - |
23 | 15 | <span class="hljs-comment">/** |
24 | 16 | * <span class="hljs-doctag">@title</span> Tree with flat nodes |
25 | 17 | */</span> |
|
30 | 22 | <span class="hljs-attr">changeDetection</span>: <span class="hljs-title class_">ChangeDetectionStrategy</span>.<span class="hljs-property">OnPush</span>, |
31 | 23 | }) |
32 | 24 | <span class="hljs-keyword">export</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">TreeFlatOverviewExample</span> { |
33 | | - <span class="hljs-keyword">private</span> _transformer = <span class="hljs-function">(<span class="hljs-params"><span class="hljs-attr">node</span>: <span class="hljs-title class_">FoodNode</span>, <span class="hljs-attr">level</span>: <span class="hljs-built_in">number</span></span>) =></span> { |
34 | | - <span class="hljs-keyword">return</span> { |
35 | | - <span class="hljs-attr">expandable</span>: !!node.<span class="hljs-property">children</span> && node.<span class="hljs-property">children</span>.<span class="hljs-property">length</span> > <span class="hljs-number">0</span>, |
36 | | - <span class="hljs-attr">name</span>: node.<span class="hljs-property">name</span>, |
37 | | - <span class="hljs-attr">level</span>: level, |
38 | | - }; |
39 | | - }; |
40 | | - |
41 | | - treeControl = <span class="hljs-keyword">new</span> <span class="hljs-title class_">FlatTreeControl</span><<span class="hljs-title class_">ExampleFlatNode</span>>( |
42 | | - <span class="hljs-function"><span class="hljs-params">node</span> =></span> node.<span class="hljs-property">level</span>, |
43 | | - <span class="hljs-function"><span class="hljs-params">node</span> =></span> node.<span class="hljs-property">expandable</span>, |
44 | | - ); |
45 | | - |
46 | | - treeFlattener = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MatTreeFlattener</span>( |
47 | | - <span class="hljs-variable language_">this</span>.<span class="hljs-property">_transformer</span>, |
48 | | - <span class="hljs-function"><span class="hljs-params">node</span> =></span> node.<span class="hljs-property">level</span>, |
49 | | - <span class="hljs-function"><span class="hljs-params">node</span> =></span> node.<span class="hljs-property">expandable</span>, |
50 | | - <span class="hljs-function"><span class="hljs-params">node</span> =></span> node.<span class="hljs-property">children</span>, |
51 | | - ); |
52 | | - |
53 | | - dataSource = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MatTreeFlatDataSource</span>(<span class="hljs-variable language_">this</span>.<span class="hljs-property">treeControl</span>, <span class="hljs-variable language_">this</span>.<span class="hljs-property">treeFlattener</span>); |
| 25 | + dataSource = <span class="hljs-variable constant_">EXAMPLE_DATA</span>; |
54 | 26 |
|
55 | | - <span class="hljs-title function_">constructor</span>(<span class="hljs-params"></span>) { |
56 | | - <span class="hljs-variable language_">this</span>.<span class="hljs-property">dataSource</span>.<span class="hljs-property">data</span> = <span class="hljs-variable constant_">EXAMPLE_DATA</span>; |
57 | | - } |
| 27 | + childrenAccessor = <span class="hljs-function">(<span class="hljs-params"><span class="hljs-attr">node</span>: <span class="hljs-title class_">FoodNode</span></span>) =></span> node.<span class="hljs-property">children</span> ?? []; |
58 | 28 |
|
59 | | - hasChild = <span class="hljs-function">(<span class="hljs-params"><span class="hljs-attr">_</span>: <span class="hljs-built_in">number</span>, <span class="hljs-attr">node</span>: <span class="hljs-title class_">ExampleFlatNode</span></span>) =></span> node.<span class="hljs-property">expandable</span>; |
| 29 | + hasChild = <span class="hljs-function">(<span class="hljs-params"><span class="hljs-attr">_</span>: <span class="hljs-built_in">number</span>, <span class="hljs-attr">node</span>: <span class="hljs-title class_">FoodNode</span></span>) =></span> !!node.<span class="hljs-property">children</span> && node.<span class="hljs-property">children</span>.<span class="hljs-property">length</span> > <span class="hljs-number">0</span>; |
60 | 30 | } |
61 | 31 |
|
62 | 32 | <span class="hljs-keyword">const</span> <span class="hljs-attr">EXAMPLE_DATA</span>: <span class="hljs-title class_">FoodNode</span>[] = [ |
|
0 commit comments