数据
treeData: [{
No: 1,
Name: '',
NodeList: [
{
No: 2,
Name: '鞋子',
NodeList: []
},
{
No: 3,
Name: '包包',
NodeList: [
{
No: 4,
Name: '大包',
NodeList: [
{
No: 7,
Name: '女包',
NodeList: [
{
No: 8,
Name: '托特包',
NodeList: [
{
No: 11,
Name: '子母包',
NodeList: [
{
No: 2,
Name: '水桶子母包',
NodeList: []
}
]
}
]
},
{
No: 10,
Name: '书包',
NodeList: []
}
]
}
]
},
{
No: 5,
Name: '中包',
NodeList: [
]
},
{
No: 6,
Name: '小包',
NodeList: [
]
}
]
}
]
}]
第一种改造:键名改造
getTreeData(list){
this.treeData = _.map(list, (item) => {
return this.setCheckedData(item)
})
},
// 将树形结构改造成自己想要的属性名
setCheckedData(node) {
let response = _.map(node, item => (
{
id: item.No,
label: item.Name,
children:item.Nodelist
}
))
_.each(response, m => {
if(m.children && m.children.length > 0){
m.children = getNewTreeDataName (m.children)
}
})
return response
},
第二种 将树形数据拉平成数组对象
getTreeData(list){
let arr = []
_.each(list, (item, i) => {
arr[i] = []
return this.setCheckedData(item, arr[i])
})
},
// 将树形结构改造成自己想要的属性名
setCheckedData(node, arr) {
//当前node没有children,意味着node没有子级,则将当前node的数据保存到数组中
arr.push({
id: node.id,
label: node.Name
});
if(node.categoryNodeList && node.categoryNodeList.length>0) {
node.categoryNodeList.forEach((subnode) => this.getCheckedData(subnode, arr));
}
},
第三种给叶子添加属性
//递归获取最后一级叶子
getTreeData(list){
this.treeData = _.map(list, (item) => {
let key = item.No
return this.setTreeData(item, key)
})
},
setTreeData(node, key) {
if(!node.NodeList|| node.NodeList.length === 0){
node.parent = key
}
if(node.NodeList && node.NodeList.length>0) {
node.NodeList.forEach((subnode) => this.setTreeData(subnode, key));
}
return node
},