【问题标题】:PrimeNG Treetable programmatically expand all nodes not workingPrimeNG Treetable 以编程方式扩展所有不工作的节点
【发布时间】:2020-07-18 01:36:30
【问题描述】:

我正在尝试递归扩展 Primeng TreeTable 组件上的所有节点,但没有成功。 行图标发生变化,没有任何反应,除非我手动单击展开/折叠图标并且所有节点都相应地展开/折叠。

这里是我想要做的事情的 stackbliz 链接,Stackblitz code

这是我的展开/折叠方法:

public expandAll(): void {
    this.files1.forEach(node => {
        this.expandCollapseRecursive(node, true);
    });
}

public collapseAll(): void {
    this.files1.forEach(node => {
        this.expandCollapseRecursive(node, false);
    });
}

private expandCollapseRecursive(node: TreeNode, isExpand: boolean): void {
    node.expanded = isExpand;
    if (node.children) {
        node.children.forEach(childNode => {
            this.expandCollapseRecursive(childNode, isExpand);
        });
    }
}

【问题讨论】:

    标签: angular primeng


    【解决方案1】:

    我找到了一个适合我的解决方案,使用LodashcloneDeep,从 TreeNode 数组进行深度克隆,然后在覆盖“this.files1”属性之前在克隆的数组中进行所有更改,然后一切正常。

    public expandAll(): void {
        const temp = cloneDeep(this.files1);
        temp.forEach(node => {
            this.expandCollapseRecursive(node, true);
        });
        this.files1 = temp;
    }
    
    public collapseAll(): void {
        const temp = cloneDeep(this.files1);
        temp.forEach(node => {
            this.expandCollapseRecursive(node, false);
        });
        this.files1 = temp;
    }
    
    private expandCollapseRecursive(node: TreeNode, isExpand: boolean): void {
        node.expanded = isExpand;
        if (node.children) {
            node.children.forEach(childNode => {
                this.expandCollapseRecursive(childNode, isExpand);
            });
        }
    }
    

    但是有更好的答案吗?

    【讨论】:

      猜你喜欢
      • 2023-03-10
      • 2019-01-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多