清晰设计树视图递归问题
Clarity Design Tree View Recursive Issue
我已经开始使用 Clarity Design Angular 项目并且 运行 遇到了 0.10.0-alpha 中提供的树视图递归模板的问题。
https://plnkr.co/edit/KK8rVH1xUGCO7VetgomA?p=preview
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" }
}
]
}
]
};
当递归检查命中不包含数组的匹配项(但只是一个 object - 参见 A1 > B3 > E3 > F1)时,它无法呈现该项目并导致任何错误单击插入符号后,可折叠部分会重复 child 个项目。
如果发送 JSON 的 API 没有将 children 放入只有一个实例的数组中,则不确定如何解决此问题。递归应该考虑只有一个 child 存在的实例(并且不在数组中)。
Add one more conditional recursion for the object case in https://plnkr.co/edit/KK8rVH1xUGCO7VetgomA?p=preview
replace this code in "recursive-selectable-structure" component
@Component({
selector: "recursive-selectable-structure",
template: `
<clr-tree-node [(clrSelected)]="item.selected">
{{item.name}}
<recursive-selectable-structure *ngIf="item && item.children && !item.children[0]" [item]="item.children">
</recursive-selectable-structure>
<ng-template
[clrIfExpanded]="item.expanded"
*ngFor="let child of item.children">
<recursive-selectable-structure
[item]="child">
</recursive-selectable-structure>
</ng-template>
</clr-tree-node>
`
})
在同一个 plunker 中替换并尝试此案例
我能够从 Raja Modamed 的输入中解决它,并在递归位上添加了一个检查以防止它在没有数组的情况下访问子级(这防止了 ngFor 错误和 returns 功能切换到开关)。
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>
`
})
现在有一个清晰的解决方案。
您可以使用清晰度递归树
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;
我已经开始使用 Clarity Design Angular 项目并且 运行 遇到了 0.10.0-alpha 中提供的树视图递归模板的问题。
https://plnkr.co/edit/KK8rVH1xUGCO7VetgomA?p=preview
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" }
}
]
}
]
};
当递归检查命中不包含数组的匹配项(但只是一个 object - 参见 A1 > B3 > E3 > F1)时,它无法呈现该项目并导致任何错误单击插入符号后,可折叠部分会重复 child 个项目。
如果发送 JSON 的 API 没有将 children 放入只有一个实例的数组中,则不确定如何解决此问题。递归应该考虑只有一个 child 存在的实例(并且不在数组中)。
Add one more conditional recursion for the object case in https://plnkr.co/edit/KK8rVH1xUGCO7VetgomA?p=preview replace this code in "recursive-selectable-structure" component
@Component({
selector: "recursive-selectable-structure",
template: `
<clr-tree-node [(clrSelected)]="item.selected">
{{item.name}}
<recursive-selectable-structure *ngIf="item && item.children && !item.children[0]" [item]="item.children">
</recursive-selectable-structure>
<ng-template
[clrIfExpanded]="item.expanded"
*ngFor="let child of item.children">
<recursive-selectable-structure
[item]="child">
</recursive-selectable-structure>
</ng-template>
</clr-tree-node>
`
})
在同一个 plunker 中替换并尝试此案例
我能够从 Raja Modamed 的输入中解决它,并在递归位上添加了一个检查以防止它在没有数组的情况下访问子级(这防止了 ngFor 错误和 returns 功能切换到开关)。
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>
`
})
现在有一个清晰的解决方案。 您可以使用清晰度递归树
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;