Skip to content

Commit 8ca674a

Browse files
committed
docs(tree): fix dynamic tree demo
1 parent d5cd0d6 commit 8ca674a

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 */
@@ -98,16 +99,21 @@ export class DynamicDataSource {
9899
if (!children || index < 0) { // If no children, or cannot find the node, no op
99100
return;
100101
}
102+
node.isLoading = true;
101103

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

110-
// notify the change
111-
this.dataChange.next(this.data);
113+
// notify the change
114+
this.dataChange.next(this.data);
115+
node.isLoading = false;
116+
}, 1000);
112117
}
118+
113119
}

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
@@ -10,5 +10,8 @@
1010
</mat-icon>
1111
</button>
1212
{{node.item}}
13+
<mat-progress-bar *ngIf="node.isLoading"
14+
mode="indeterminate"
15+
class="demo-tree-progress-bar"></mat-progress-bar>
1316
</mat-tree-node>
1417
</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';
@@ -35,6 +36,7 @@ import {LoadmoreTreeDemo} from './loadmore-tree-demo/loadmore-tree-demo';
3536
MatIconModule,
3637
MatInputModule,
3738
MatTreeModule,
39+
MatProgressBarModule,
3840
],
3941
declarations: [
4042
ChecklistTreeDemo,

0 commit comments

Comments
 (0)