如何在angular 8中使用mat树的嵌套树控件扩展特定child节点的所有parent节点?

How to expand all parent node of particular child node using nested tree control of mat tree in angular 8?


getBookOutlineBar 方法将获取一个节点列表和在大纲侧边栏打开时需要选择的选定节点。我已经实施了扩展方法来扩展所有 parent 节点,但它只扩展一个特定节点,而不是所有 parent 节点。如果打开页面 "Heading" 并且我单击大纲边栏,那么所有 parent 节点都应该展开并且应该选择标题。我在angular8中使用了mat树的嵌套树控件。(我的代码中使用了这个例子:https://stackblitz.com/angular/dklgxbrynad?file=src%2Fapp%2Ftree-nested-overview-example.ts)。请提出任何帮助。


getBookOutlineBar() {
        let customURL = '';
        this.bookService.GetBookOutlineBar(this.bookId, location.hash).subscribe(data => {
          this.dataSource.data = data.list;
          if (data.selectedNode != null) {
            this.selectedNode = data.selectedNode;
            this.treeControl.collapseAll();
            this.expand(this.dataSource.data, this.selectedNode.UniqueId);
          }    
        })
      }
    expand(data: BookOutlineBar[], uniqueId: string): any {
        data.forEach(node => {
          if (node.Children && node.Children.find(c => c.UniqueId === uniqueId)) {
            this.treeControl.expand(node);
            this.expand(this.treeControl.dataNodes, node.UniqueId);
          }
          else if (node.Children && node.Children.find(c => c.Children)) {
            this.expand(node.Children, uniqueId);
          }
        });
      }

您的代码

 if (node.Children && node.Children.find(c => c.UniqueId === uniqueId)) {
            this.treeControl.expand(node);
            this.expand(this.treeControl.dataNodes, node.UniqueId);
          }

替换为

 if (node.Children && node.Children.find(c => c.UniqueId === uniqueId)) {
            this.treeControl.expand(node);
            this.expand(this.dataSource.data, node.UniqueId);
          }