这个读懂代码也挺简单的
html代码
<nz-tree [nzData]="nodes" nzAsyncData (nzClick)="nzEvent($event)" (nzExpandChange)="nzEvent($event)"> </nz-tree>
nzExpandChange 是点击展开树节点图标触发事件
nzClick 就不用介绍了吧
点击之后 会返回这些
eventName 就是你触发的事件名 click 和 Expand 下拉
nzEvent事件的js
nzEvent(event: NzFormatEmitEvent): void {
if (event.eventName === 'expand') {
const node = event.node;
this.basicInformation(event.keys) // 注意传入的 keys 是个数组
if (node?.getChildren().length === 0 && node?.isExpanded) {
this.loadNode().then(data => { //调用addChildren赋值
node.addChildren(data);
});
}
}
}
basicInformation 自己写的获取数据的 js
basicInformation(node) {
let nodeType = ''
if (node) {
nodeType = node.pop() //拿到的数组取到最后一项
}
this.rendererMeta.executeAction({ //内部获取数据方法
params: {
node: nodeType
},
}).then(res => {
console.log(res);
let nodes = []
let content = {
title: '',
key: '',
isLeaf: ''
}
res.items.forEach(element => {
content.title = element.properties.name
content.key = element.id
content.isLeaf = element.leaf
nodes.push(content)
content = {
title: '',
key: '',
isLeaf: ''
}
});
if (node) { //第一层默认不传值 如果没有就一定是 nodesChildren
this.nodesChildren = nodes
} else {
this.nodes = nodes
console.log(this.nodes);
}
})
}
loadNode代码
loadNode(): Promise<NzTreeNodeOptions[]> {
return new Promise(resolve => {
setTimeout(
() =>
resolve(this.nodesChildren),//直接成功就返回nodesChildren
1000
);
});
}
我写的方法比较笨吧 仅供参考 有优化方法欢迎交流学习 提升一下嘛