清晰度设计树视图递归问题

时间:2017-08-13 05:24:00

标签: angular vmware clarity vmware-clarity

我已经开始使用Clarity Design Angular项目,并且遇到了0.10.0-alpha中提供的Tree View递归模板的问题。

here

    selectableRoot = {
    "@name": "A1",
    "selected": false,
    "expanded": true,
    "children": [
        {
            "@name": "B1",
            "selected": false,
            "children": [
                { "@name": "C1" },
                { "@name": "C2" },
                { "@name": "C3" }
            ]
        },
        {
            "@name": "B2",
            "selected": true,
            "expanded": true,
            "children": [
                { "@name": "D1" },
                {
                    "@name": "D2",
                    "selected": false
                },
                { "@name": "D3" }
            ]
        },
        {
            "@name": "B3",
            "selected": true,
            "children": [
                { "@name": "E1" },
                { "@name": "E2" },
                { 
                  "@name": "E3",
                  "children":
                     { "@name": "F1" }
                }
            ]
        }
    ]
};

当递归检查命中一个不包含数组的匹配(但只是一个对象 - 参见A1> B3> E3> F1)时,它无法呈现该项并导致任何可折叠部分重复的错误点击插入符号后的子项目。

如果只有一个实例,如果发送JSON的API没有将子节点放入数组中,则不确定如何解决此问题。递归应该考虑只存在一个子节点(并且不在数组中)的实例。

3 个答案:

答案 0 :(得分:4)

我能够从Raja Modamed的输入中解决它,并在递归位上再添加一个检查,以防止它在没有数组的情况下访问子节点(这会阻止ngFor错误并将功能返回到切换)。 / p>

https://plnkr.co/edit/OAujF19FPDqaEWvFfgN1?p=preview

@Component({
selector: "recursive-selectable-structure",
template: `
    <clr-tree-node [(clrSelected)]="item.selected">
        {{item.name}}
        <span *ngIf="item.children?.length > 0">
          <ng-template 
            [clrIfExpanded]="item.expanded" 
            *ngFor="let child of item.children">
              <recursive-selectable-structure
                  [item]="child">
              </recursive-selectable-structure>
          </ng-template>
        </span>
        <ng-template *ngIf="item && item.children && !item.children[0]" [clrIfExpanded]="item.children.expanded">
          <recursive-selectable-structure [item]="item.children">
          </recursive-selectable-structure>
        </ng-template>
    </clr-tree-node>
`

})

答案 1 :(得分:1)

  

https://plnkr.co/edit/KK8rVH1xUGCO7VetgomA?p=preview中为对象案例添加一个条件递归   在“recursive-selectable-structure”组件中替换此代码

found

})

在同一个plunker中替换并尝试这种情况

答案 2 :(得分:0)

现在有一个清晰度解决方案。 您可以使用清晰度递归树

https://clarity.design/documentation/tree-view#recursive-tree

<clr-tree>
  <clr-tree-node *clrRecursiveFor="let item of items; getChildren: getItemChildren"
                                 [(clrSelected)]="item.selected">
        {{item.name}}
  </clr-tree-node>
</clr-tree>

您还需要在组件中定义一个将获得子项的函数

getItemChildren = (item) => item.children;