【发布时间】:2019-12-17 21:19:31
【问题描述】:
我有一个数组,它可以包含未知数量的项目和无限嵌套。我需要能够为每个父级添加一个按钮来切换子级的显示,并且每个子级都可以有一个按钮来切换其子级。此外,每个孩子都有一个可以关闭父母的按钮。
我无法让开关正常工作。我现在拥有它的方式是基于数组的索引,对于每组子元素,它从 0 开始,如果它们具有相同的索引,这可能会导致多个项目同时打开。
我在这里有一个关于 Stackblitz 问题的示例:https://stackblitz.com/edit/angular-nested-menu-toggle?file=src%2Fapp%2Fapp.component.html
您可以通过单击按钮“five”,然后单击“five.two”按钮来查看问题。您可以看到它同时打开“five.two”以及“two”和“two.two”,因为它们都具有相同的索引。
所以我需要的是能够切换一个项目,它只影响它的直接父或子,而不是同时影响其他所有东西。
这是我的数组:
myArray = [
{
'title': 'one'
},
{
'title': 'two',
'children': [
{
'title': 'two.one'
},
{
'title': 'two.two',
'children': [
{
'title': 'two.two.one'
},
{
'title': 'two.two.two'
}
]
},
{
'title': 'two.three',
'children': [
{
'title': 'two.three.one'
},
{
'title': 'two.three.two'
}
]
}
]
},
{
'title': 'three',
'children': [
{
'title': 'three.one'
},
{
'title': 'three.two',
'children': [
{
'title': 'three.two.one'
},
{
'title': 'three.two.two'
}
]
},
{
'title': 'three.three',
'children': [
{
'title': 'three.three.one'
},
{
'title': 'three.three.two'
}
]
}
]
},
{
'title': 'four'
},
{
'title': 'five',
'children': [
{
'title': 'five.one'
},
{
'title': 'five.two'
}
]
},
{
'title': 'six',
'children': [
{
'title': 'six.one'
},
{
'title': 'six.two',
'children': [
{
'title': 'six.two.one'
},
{
'title': 'six.two.two'
}
]
},
{
'title': 'six.three',
'children': [
{
'title': 'six.three.one'
},
{
'title': 'six.three.two'
}
]
}
]
}
];
然后我在构造函数中有一个切换变量:
constructor() {
this.toggle = this.myArray.map(i => false);
}
最后,我的 HTML
<ul>
<li *ngFor="let item of myArray; let i = index">
<button (click)="toggle[i] = !toggle[i]">{{item.title}}</button>
<div *ngIf="item.children && toggle[i]">
<ng-container *ngTemplateOutlet="tree; context: { $implicit: item.children, idx: i }"></ng-container>
</div>
</li>
</ul>
<ng-template #tree let-allItems let-idx="idx">
<ul>
<li *ngFor="let item of allItems; let n = index">
<button (click)="toggle[idx] = !toggle[idx]">X</button>
<button (click)="toggle[n] = !toggle[n]">{{item.title}}</button>
<div *ngIf="item.children && toggle[n]">
<ng-container *ngTemplateOutlet="tree; context: { $implicit: item.children, idx: n }"></ng-container>
</div>
</li>
</ul>
</ng-template>
谢谢!
【问题讨论】:
标签: angular