0

我需要替换树上的一些数据,然后运行 ​​treemodel.update() ,然后选择一个节点。树在那个时候已经被渲染了。这是我的代码:

    @ViewChild('resulttree') resulttree: ElementRef;
    
    updateResulttree(id: number) {
        for (let node of this.resultNodes) {
          node.children[0].children = node.children[0].children.filter(childnode => childnode.selected);
          node.children[1].children = node.children[1].children.filter(childnode => childnode.selected);
        }        
    
        this.resulttree.treeModel.update();
    
        setTimeout(() => {
            const someNode = this.resulttree.treeModel.getNodeBy(node => node.data.tree_node_id === id);
              someNode.setActiveAndVisible();
            }, 100); 
}

这个带有 setTimeout 的解决方法有效,但效果不好。如果 update() 方法耗时超过 100 毫秒,“someNode”将未定义。

任何想法如何使它更好?

Angular 10,AngularTreeCompoment 10.0.2 https://angular2-tree.readme.io/docs/getting-started

4

1 回答 1

0

从他们的代码https://github.com/CirclonGroup/angular-tree-component/blob/master/projects/angular-tree-component/src/lib/models/tree.model.ts,调用 update() 函数将完成后触发 updateData 事件,只要它不是第一次更新(如果您已经有渲染树,则不应该如此),您可以在调用 update() 之前订阅该事件并在事件处理程序中选择您的节点(不要忘记取消订阅):

  @action update() {
    // Rebuild tree:
    let virtualRootConfig = {
      id: this.options.rootId,
      virtual: true,
      [this.options.childrenField]: this.nodes
    };

    this.dispose();

    this.virtualRoot = new TreeNode(virtualRootConfig, null, this, 0);

    this.roots = this.virtualRoot.children;

    // Fire event:
    if (this.firstUpdate) {
      if (this.roots) {
        this.firstUpdate = false;
        this._calculateExpandedNodes();
      }
    } else {
      this.fireEvent({ eventName: TREE_EVENTS.updateData });
    }
  }
于 2021-01-15T14:07:14.880 回答