树形地址,点击获取子级,获取所有父级名称,并拼接拿到全称地址

treeFindPath(tree, parentCode) {
      let path = [];
       if (!tree) return [];
       let forFn = function (tree, parentCode) {
         for (const element of tree) {
         // 存放最后返回的内容,返回text集合
         let data = element;
         path.push(data.label);
         if (data.areaCode === parentCode) return path;
         if (data.children) {
             const findChildren = forFn(data.children,parentCode);
             if (findChildren) return findChildren
         }
         path.pop()
         }
       }
         forFn(tree, parentCode);
         return path;
 },

调用
···
let names=this.treeFindPath(this.areaList,this.queryForm.parentCode)
let name=‘’
// 拼接拿到全称地址
names.forEach(item=>{
name += item
})
this.address = name + this.queryForm.name
console.log(‘address’,this.address)
···

猜你喜欢

转载自blog.csdn.net/qq_26841153/article/details/128459664