如果children为空数组,则children为 undefined
getTree() {
this.$get('/distribution/pc/org/getTree/' + 0 + '/' + 0, {}).then((res) => {
if (res.code == 1000) {
this.options = this.getTreeData(res.data); //获取树结构值
res.data =[{
label: '一级 1',
children: [{
label: '二级 1-1',
children: []
}]
}, {
label: '一级 2',
children: [{
label: '二级 2-1',
children: [{
label: '三级 2-1-1'
}]
}, {
label: '二级 2-2',
children: [{
label: '三级 2-2-1'
}]
}]
}, {
label: '一级 3',
children: []
}]
this.getTreeData(res.data); //获取树结构值
}
})
},
getTreeData(data) {
// 循环遍历json数据
for (var i = 0; i < data.length; i++) {
if (data[i].children.length < 1) {
// children若为空数组,则将children设为undefined
data[i].children = undefined;
} else {
// children若不为空数组,则继续 递归调用 本方法
this.getTreeData(data[i].children);
}
}
return data;
},
|