Skip to content

Commit 8c3ba34

Browse files
tinayuangaojosephperrott
authored andcommitted
docs(tree): fix dynamic tree demo (#10384)
1 parent 67c7348 commit 8c3ba34

File tree

4 files changed

+29
-15
lines changed

4 files changed

+29
-15
lines changed

src/demo-app/tree/dynamic-tree-demo/dynamic-database.ts

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {map} from 'rxjs/operators';
1515

1616
/** Flat node with expandable and level information */
1717
export class DynamicFlatNode {
18+
isLoading: boolean = false;
1819
constructor(public item: string, public level: number = 1, public expandable: boolean = false) {}
1920
}
2021

@@ -69,14 +70,14 @@ export class DynamicDataSource {
6970
private database: DynamicDatabase) {}
7071

7172
connect(collectionViewer: CollectionViewer): Observable<DynamicFlatNode[]> {
72-
return merge(collectionViewer.viewChange, this.treeControl.expansionModel.onChange!)
73-
.pipe(map((change) => {
74-
if ((change as SelectionChange<DynamicFlatNode>).added ||
73+
this.treeControl.expansionModel.onChange!.subscribe(change => {
74+
if ((change as SelectionChange<DynamicFlatNode>).added ||
7575
(change as SelectionChange<DynamicFlatNode>).removed) {
76-
this.handleTreeControl(change as SelectionChange<DynamicFlatNode>);
77-
}
78-
return this.data;
79-
}));
76+
this.handleTreeControl(change as SelectionChange<DynamicFlatNode>);
77+
}
78+
});
79+
80+
return merge(collectionViewer.viewChange, this.dataChange).pipe(map(() => this.data));
8081
}
8182

8283
/** Handle expand/collapse behaviors */
@@ -99,16 +100,21 @@ export class DynamicDataSource {
99100
if (!children || index < 0) { // If no children, or cannot find the node, no op
100101
return;
101102
}
103+
node.isLoading = true;
102104

103-
if (expand) {
104-
const nodes = children.map(name =>
105+
setTimeout(() => {
106+
if (expand) {
107+
const nodes = children.map(name =>
105108
new DynamicFlatNode(name, node.level + 1, this.database.isExpandable(name)));
106-
this.data.splice(index + 1, 0, ...nodes);
107-
} else {
108-
this.data.splice(index + 1, children.length);
109-
}
109+
this.data.splice(index + 1, 0, ...nodes);
110+
} else {
111+
this.data.splice(index + 1, children.length);
112+
}
110113

111-
// notify the change
112-
this.dataChange.next(this.data);
114+
// notify the change
115+
this.dataChange.next(this.data);
116+
node.isLoading = false;
117+
}, 1000);
113118
}
119+
114120
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,8 @@
1111
</mat-icon>
1212
</button>
1313
{{node.item}}
14+
<mat-progress-bar *ngIf="node.isLoading"
15+
mode="indeterminate"
16+
class="demo-tree-progress-bar"></mat-progress-bar>
1417
</mat-tree-node>
1518
</mat-tree>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.demo-tree-progress-bar {
2+
margin-left: 30px;
3+
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
MatFormFieldModule,
1717
MatIconModule,
1818
MatInputModule,
19+
MatProgressBarModule,
1920
MatTreeModule
2021
} from '@angular/material';
2122
import {TreeDemo} from './tree-demo';
@@ -36,6 +37,7 @@ import {LoadmoreTreeDemo} from './loadmore-tree-demo/loadmore-tree-demo';
3637
MatIconModule,
3738
MatInputModule,
3839
MatTreeModule,
40+
MatProgressBarModule,
3941
],
4042
declarations: [
4143
ChecklistNestedTreeDemo,

0 commit comments

Comments
 (0)