Skip to content

Commit 5bb02e8

Browse files
author
Your Name
committed
Angular Material In Depth
1 parent 492ee3d commit 5bb02e8

File tree

4 files changed

+61
-2
lines changed

4 files changed

+61
-2
lines changed

src/app/app.component.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@
2020
<span>Drag and Drop</span>
2121
</a>
2222

23+
<a mat-list-item routerLink="tree-demo">
24+
<mat-icon>park</mat-icon>
25+
<span>Tree Demo</span>
26+
</a>
27+
2328
<a mat-list-item>
2429
<mat-icon>person_add</mat-icon>
2530
<span>Register</span>

src/app/tree-demo/tree-demo.component.html

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,49 @@
33

44
<h3>Nested Tree Demo</h3>
55

6+
<mat-tree
7+
class="example-tree mat-elevation-z4"
8+
[dataSource]="nestedDataSource" [treeControl]="nestedTreeControl">
9+
10+
<mat-tree-node *matTreeNodeDef="let node" matTreeNodeToggle>
11+
12+
{{node.name}}
13+
14+
</mat-tree-node>
15+
16+
17+
<mat-nested-tree-node *matTreeNodeDef="let node; when: hasNestedChild">
18+
19+
<div class="mat-tree-node">
20+
21+
<button mat-icon-button matTreeNodeToggle>
22+
23+
<mat-icon>
24+
25+
{{ nestedTreeControl.isExpanded(node) ? "expand_more": "chevron_right" }}
26+
27+
</mat-icon>
28+
29+
</button>
30+
31+
{{node.name}}
32+
33+
</div>
34+
35+
<div class="nested-node"
36+
[class.example-tree-invisible]="!nestedTreeControl.isExpanded(node)">
37+
38+
<ng-container matTreeNodeOutlet></ng-container>
39+
40+
</div>
41+
42+
</mat-nested-tree-node>
43+
44+
45+
</mat-tree>
46+
47+
48+
649
<h3>Flat Tree Demo</h3>
750

851
</div>

src/app/tree-demo/tree-demo.component.scss

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
/*
3030
* This padding sets alignment of the nested nodes.
3131
*/
32-
.example-tree .mat-nested-tree-node div[role=group] {
32+
.example-tree .mat-nested-tree-node .nested-node {
3333
padding-left: 40px;
3434
}
3535

@@ -38,6 +38,6 @@
3838
* Leaf nodes need to have padding so as to align with other non-leaf nodes
3939
* under the same parent.
4040
*/
41-
.example-tree div[role=group] > .mat-tree-node {
41+
.example-tree .nested-node > .mat-tree-node {
4242
padding-left: 40px;
4343
}

src/app/tree-demo/tree-demo.component.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,22 @@ const TREE_DATA: CourseNode[] = [
5858
})
5959
export class TreeDemoComponent implements OnInit {
6060

61+
nestedDataSource = new MatTreeNestedDataSource<CourseNode>();
62+
63+
nestedTreeControl = new NestedTreeControl<CourseNode>(node => node.children);
64+
65+
6166
ngOnInit() {
6267

68+
this.nestedDataSource.data = TREE_DATA;
69+
6370

6471
}
6572

73+
hasNestedChild(index: number, node:CourseNode) {
74+
return node?.children?.length > 0;
75+
}
76+
6677
}
6778

6879

0 commit comments

Comments
 (0)