我用角材料和角9创建树。
但是我有一个问题。我的物品中有物品,是否有孩子。
当我创建树时,没有孩子的每个项目都会被预先选择,但是我不需要这样做。
rolesToTree(roles): void {
console.log(JSON.stringify(roles))
const selections: ItemFlatNode[] = [];
const controllers = [];
roles.forEach(element => {
const attrs = [];
element['children'].forEach(attr => {
const secAttrs = [];
attr['children'].forEach(sec => {
const secAction = { name: sec['title'], actionId: sec['id'] };
secAttrs.push(secAction);
if (sec['selected'] === true) {
const a = new ItemFlatNode(false, sec['id'], sec['title'], 3);
selections.push(a);
}
});
if (attr['selected'] === true) {
const a = new ItemFlatNode(false, attr['id'], attr['title'], 2);
selections.push(a);
}
const actionss = { name: attr['title'], actionId: attr['id'], children: secAttrs };
attrs.push(actionss);
});
const controller = { name: element['title'], actionId: element['id'], children: attrs };
controllers.push(controller);
});
// this.checklistSelection = new SelectionModel<ItemFlatNode>(true, selections);
this.defualtSelected = selections;
const data = [{ name: 'All', actionId: 'sds', children: controllers }];
this.database.dataChange.next(this.database.builTree(data, 0));
}
是什么问题?我该如何解决这个问题?
答案 0 :(得分:0)
方法DescendantsAllSelected会验证是否选中了每个子节点,如果没有后代元素,则返回true。
要解决此问题,需要检查后代长度,如果该长度为0,则返回节点节点选择的值。
更新如下的despantsantsAllSelected方法:
descendantsAllSelected(node: ItemFlatNode): boolean {
const descendants = this.treeControl.getDescendants(node);
return (this.checklistSelection.isSelected(node) && descendants.length==0) || (descendants.length>0 && descendants.every(child => this.checklistSelection.isSelected(child)));
}