Cra*_*aig 5 angular-material angular
我正在实现角度/材质树组件,并且遇到一些问题。保存对树的更改后,我正在重置支持树的数据(有效),但是应用程序变得异常缓慢,扩展节点可能需要8秒钟。
使这个问题变得更奇怪的是,实际上操纵数据源的代码是在其他地方运行的,例如向树中添加新的子级-我们希望更新UI,这不会引起问题。只有在保存时,应用程序才会变慢。
save(): void {
const trees = this.flattenedTree.filter(node => this.isRootNode(node));
this.serviceThatHasNoSideEffects.save(trees)
.then(result => {
this.tree = result;
this.flattenedTree = this.getFlattenedTree();
this.refreshTree();
});
}
private refreshTree() {
const data: any[] = this.flattenedTree
.filter(x => this.isRootNode(x))
.filter(x => x.children.length > 0 || x.id === this.focusId);
this.nestedDataSource.data = null;
this.nestedDataSource.data = data;
const focusNode = this.getNode(this.focusId);
this.nestedTreeControl.expand(focusNode);
}
private addChild(parentNode: any, childNode: any) {
if (!this.getNode(parentNode)) {
this.flattenedTree.push(parentNode);
}
if (!this.getNode(childNode)) {
this.flattenedTree.push(childNode);
}
parentNode.children.push(childNode);
this.refreshTree();
this.nestedTreeControl.expand(parentNode);
}
Run Code Online (Sandbox Code Playgroud)
编辑:
更改刷新树以创建全新的数据源可解决缓慢的问题(内存泄漏?),但不会在UI中添加未显示的子对象。虽然孩子在展平的树上,但是应该显示出来。
private refreshTree() {
const data: any[] = this.flattenedTree
.filter(x => this.isRootNode(x))
.filter(x => x.children.length > 0 || x.id === this.focusId);
this.nestedDataSource = new MatTreeNestedDataSource<theTreeType>();
this.nestedDataSource.data = data;
const focusNode = this.getNode(this.focusId);
this.nestedTreeControl.expand(focusNode);
}
Run Code Online (Sandbox Code Playgroud)
编辑:这是支持它的html。很标准。
<mat-tree-node *matTreeNodeDef="let node" matTreeNodeToggle>
<li class="mat-tree-node">
<button mat-icon-button disabled></button>
{{node.uniqueName}}
</li>
</mat-tree-node>
<!--when has nested child-->
<mat-nested-tree-node *matTreeNodeDef="let node; when: hasNestedChild">
<li>
<div class="mat-tree-node">
<button mat-icon-button matTreeNodeToggle>
<mat-icon>
{{nestedTreeControl.isExpanded(node) ? 'expand_more' : 'chevron_right'}}
</mat-icon>
</button>
{{node.uniqueName}}
</div>
<ul [class.invisible]="!nestedTreeControl.isExpanded(node)">
<ng-container matTreeNodeOutlet></ng-container>
</ul>
</li>
</mat-nested-tree-node>
</mat-tree>
Run Code Online (Sandbox Code Playgroud)
尽管接受的答案似乎提供了一些速度提升,但最终为我完成的解决方案(在 Angular 8 中)是这样的:
更改官方示例中的行:
<ul [class.example-tree-invisible]="!treeControl.isExpanded(node)">
对此:
<ul *ngIf="treeControl.isExpanded(node)">
这样折叠的子树根本不会加载到 DOM 中。
我花了几个小时才让它工作,但这是我所做的更改:
// cdk tree that mat tree is based on has a bug causing children to not be rendered in the UI without first setting the data to null
this.nestedDataSource.data = null;
// mat-tree has some sort of memory leak issue when not instantiating a new MatTreeNestedDataSource which causes the app to become very slow
this.nestedDataSource = new MatTreeNestedDataSource<LocationHierarchyNodeDataModel>();
this.nestedDataSource.data = data;
Run Code Online (Sandbox Code Playgroud)
我在这里找到的显示儿童问题:https : //github.com/angular/material2/issues/11381
| 归档时间: |
|
| 查看次数: |
2476 次 |
| 最近记录: |