diff --git a/src/demo-app/tree/dynamic-tree-demo/dynamic-database.ts b/src/demo-app/tree/dynamic-tree-demo/dynamic-database.ts index 918753d4f683..0accd6c384bd 100644 --- a/src/demo-app/tree/dynamic-tree-demo/dynamic-database.ts +++ b/src/demo-app/tree/dynamic-tree-demo/dynamic-database.ts @@ -15,6 +15,7 @@ import {map} from 'rxjs/operators'; /** Flat node with expandable and level information */ export class DynamicFlatNode { + isLoading: boolean = false; constructor(public item: string, public level: number = 1, public expandable: boolean = false) {} } @@ -69,14 +70,14 @@ export class DynamicDataSource { private database: DynamicDatabase) {} connect(collectionViewer: CollectionViewer): Observable { - return merge(collectionViewer.viewChange, this.treeControl.expansionModel.onChange!) - .pipe(map((change) => { - if ((change as SelectionChange).added || + this.treeControl.expansionModel.onChange!.subscribe(change => { + if ((change as SelectionChange).added || (change as SelectionChange).removed) { - this.handleTreeControl(change as SelectionChange); - } - return this.data; - })); + this.handleTreeControl(change as SelectionChange); + } + }); + + return merge(collectionViewer.viewChange, this.dataChange).pipe(map(() => this.data)); } /** Handle expand/collapse behaviors */ @@ -98,16 +99,21 @@ export class DynamicDataSource { if (!children || index < 0) { // If no children, or cannot find the node, no op return; } + node.isLoading = true; - if (expand) { - const nodes = children.map(name => + setTimeout(() => { + if (expand) { + const nodes = children.map(name => new DynamicFlatNode(name, node.level + 1, this.database.isExpandable(name))); - this.data.splice(index + 1, 0, ...nodes); - } else { - this.data.splice(index + 1, children.length); - } + this.data.splice(index + 1, 0, ...nodes); + } else { + this.data.splice(index + 1, children.length); + } - // notify the change - this.dataChange.next(this.data); + // notify the change + this.dataChange.next(this.data); + node.isLoading = false; + }, 1000); } + } diff --git a/src/demo-app/tree/dynamic-tree-demo/dynamic-tree-demo.html b/src/demo-app/tree/dynamic-tree-demo/dynamic-tree-demo.html index 639e6bd5f8a8..04b269ab0eba 100644 --- a/src/demo-app/tree/dynamic-tree-demo/dynamic-tree-demo.html +++ b/src/demo-app/tree/dynamic-tree-demo/dynamic-tree-demo.html @@ -10,5 +10,8 @@ {{node.item}} + diff --git a/src/demo-app/tree/dynamic-tree-demo/dynamic-tree-demo.scss b/src/demo-app/tree/dynamic-tree-demo/dynamic-tree-demo.scss index e69de29bb2d1..e857a374fed7 100644 --- a/src/demo-app/tree/dynamic-tree-demo/dynamic-tree-demo.scss +++ b/src/demo-app/tree/dynamic-tree-demo/dynamic-tree-demo.scss @@ -0,0 +1,3 @@ +.demo-tree-progress-bar { + margin-left: 30px; +} diff --git a/src/demo-app/tree/tree-demo-module.ts b/src/demo-app/tree/tree-demo-module.ts index 8e22620a42f6..ac8051b57a26 100644 --- a/src/demo-app/tree/tree-demo-module.ts +++ b/src/demo-app/tree/tree-demo-module.ts @@ -16,6 +16,7 @@ import { MatFormFieldModule, MatIconModule, MatInputModule, + MatProgressBarModule, MatTreeModule } from '@angular/material'; import {TreeDemo} from './tree-demo'; @@ -35,6 +36,7 @@ import {LoadmoreTreeDemo} from './loadmore-tree-demo/loadmore-tree-demo'; MatIconModule, MatInputModule, MatTreeModule, + MatProgressBarModule, ], declarations: [ ChecklistTreeDemo,