我目前在做带有checkbox的tree组件,我渲染的思路是从父类向子类递归,有children属性就让children调用自己,代码如下
data = [{
label: 111,
children: [{
label: 222,
}]
}, {
label: 333
}]
translate = (content, key, first) => {
content.forEach((i, index) => {
i.disabled = this.defaultDisableAll;
if (this.defaultDisabledList.length > 0) {
i.disabled = this.defaultDisabledList.indexOf(i[this.nodeKey]) > -1;
}
i.key = key + (first ? '' : '-') + (index + 1);
i.checked = this.defaultCheckedKey.indexOf(i[this.nodeKey]) > -1;
i.expanded = this.defaultExpandAll;
if (this.defaultExpandedKey.length > 0) {
i.expanded = this.defaultExpandedKey.indexOf(i[this.nodeKey]) > -1;
}
i.nodeLevel = i.key.split('-').length;
if (i.checked) {
treeData.selectedKeys.push(i.key);
}
if (i.children && i.children.length > 0) {
treeData.newDataMap[i.key] = {...i};
this.translate(i.children, i.key, false);
} else {
treeData.newNodes.push({...i});
}
})
};
ngOnInit() {
this.translate(this.data, '', true);
}
现在需要做checkbox的选择联动,从父类向子类递归的话,需要n-1次递归,感觉很影响性能,有大神给个checkbox多层嵌套联动的解决方案么
我的思路是这样的,每次点击一个checkbox的时候,向父级跟children发出通知,
初始化的时候,默认属性就通过设置属性的方式去修改默认的checked,
建立有children属性的newDataMap对象