如何通过代码扩展PrimeNg TreeTable



假设我有一个由PrimeNg为Angular2提供的树表。如何在代码中扩展特定节点(例如onNodeSelect回调)?

我想 OP 不再需要答案,但对于任何到达这里但找不到答案的人——

有一个属性expanded用于TreeNode,您需要做的就是将其设置为true

selectedNode.expanded=true;

如果您希望显示所有展开的树,只需遍历 TreeNode 并在每个节点上设置展开。 这将与此类似

  expandChildren(node:TreeNode){
    if(node.children){
      node.expanded=true;
      for(let cn of node.children){
        this.expandChildren(cn);
      }
    }
  }

执行prajeesh kumar提到的代码后,要刷新树表,请添加以下代码行:

this.treeNodeData = [...this.treeNodeData];

你可以像这样调用 expandChildren() 方法:

  expandTreeNodes() {
    if (this.treeNodeData) {
      this.treeNodeData.forEach(x => this.expandChildren(x));
      // Below line is important as it would refresh the TreeTable data,
      // which would force automatic update of nodes and since expanded
      // has been set to true on the expandChildren() method.
      this.treeNodeData = [...this.treeNodeData ]; 
    }
  }

使用上面,我刚刚编写了用于扩展/折叠整个可处理节点的函数。

在我的模板中,我将整个树表 json 传递给 exapandORcollapse

<button type="button" (click)="exapandORcollapse(basicTreeTable)">Collapse /Expand all</button>
 <p-treeTable [value]="basicTreeTable" selectionMode="single" [(selection)]="selectedPortfolio" (onNodeSelect)="nodeSelect($event)"
            (onNodeUnselect)="nodeUnselect($event)" (onRowDblclick)="onRowDblclick($event)" scrollable="true">

在我的组件.ts文件中

 exapandORcollapse(nodes) {
    for(let node of nodes) 
  { 
    if (node.children) {
        if(node.expanded == true)
          node.expanded = false;
          else
          node.expanded = true;
        for (let cn of node.children) {
            this.exapandORcollapse(node.children);
        }
    }
  }
}

我做了同样的事情,但我将相同的树对象克隆到自身并且它起作用了。

this.treeData.forEach((node) => {this.expandRecursive(node, true);});
this.treeData = _.cloneDeep(this.treeData);

相关内容

  • 没有找到相关文章