点击展开掉一次接口,拿到的是子数据,然后二级数据拼接到一级数据里,
但是三级数据如何拼接到二级数据。
//tree组件的展开方法
async expandList(data, obj, node) {
console.log(node)
console.log(data)
//默认展开的节点赋值
let flag = false
this.arr.some(item => {
if (item === data.id) {
flag = true
return true
}
})
if (!flag) {
this.arr.push(data.id)
}
// 根据父id获取下一级数据
let item = {
parentId: data.id,
}
let res = await getAllCountryList(item);
// console.log(res) //子数据
console.log(res, '1111', data.id)
this.zhan = res
let test = []
let test1 = []
// this.zhan1 刚进入页面的一级数据
for (let a of this.zhan1) {
if (a.id == data.id) {
a.children = this.zhan //子数据拼接到一级数据里边
test = this.zhan1 //此时tree展示一二级数据
}
}
// for(let b of this.zhan){
//if(b.id==data.id){
// b.children = this.zhan
//console.log(b)
//}else{
//console.log(‘hello')
//}
//}
this.treeData = dataFormat(this.options, test).list;
},
你要将三级数据拼接到二级数据中,这样在处理二级数据时将三级数据与其父级数据进行匹配,并将三级数据添加到父级数据的children属性中。
for (let a of this.zhan1) {
if (a.id == data.id) {
a.children = this.zhan //子数据拼接到一级数据里边
for (let b of a.children) {
let item = {
parentId: b.id,
}
let res = await getAllCountryList(item)
b.children = res // 将三级数据添加到二级数据的children属性中
}
test = this.zhan1 //此时tree展示一二级数据
}
}