不同编辑器中生成的树形结构的最后一级的children有三种情况:
- 为空数组[]
- 有children但被定义为undefined
- 没有children属性
// 递归判断列表,把最后的children设为undefined(递归判断参考自网络)
getTreeData(data) {
if (data.length > 0) {
for (var i = 0; i < data.length; i++) {
if (!data[i].children || data[i].children.length < 1) {
// children若为空,则将children设为undefined
data[i].children = undefined;
} else {
// children若不为空,则继续递归调用本方法
this.getTreeData(data[i].children);
}
}
}
return data;
},
// 获取部门树
getTreeselect() {
this.loading = true;
DEPT.treeselect()
.then(response => {
this.deptOptions = this.getTreeData(response.data);
console.log(response.data);
this.loading = false;
})
.catch(error => {
this.loading = false;
console.log(error);
});
},