不包含children属性的一段数组对象,前端转化成包含children的树结构数据展示

不包含children属性的一段数组对象,前端转化成包含children的树结构数据展示

实现效果

在这里插入图片描述

每点击一次列表数据,根据点击行id查询对应下级数据列表,找到点击的列表下标,将其children数据进行赋值

数据格式

[
{
“id”: “b25e633484564c76804906015e8719f8”,
“name”: “荆门市”,
“isParent”: true,
“open”: false,
“attributes”: {
“code”: “ORGAN_1042246615965896704”,
“hasElement”: true,
“status”: “1”
},
“pid”: “b58721a3-4a4c-4d28-acb3-800910834ae1”
}
]

代码

<el-cascader
  v-model="infoform.areaCode"
  placeholder="请选择所属区域"
  :options="areaList"
  :props="{
    checkStrictly: true,
    emitPath: false,
    value: 'id',
    label: 'name',
  }"
  ref="refHandle"
  clearable
  :show-all-levels="false"
  filterable
  @expand-change="handleExpand"
  @change="handleAreaChange"
></el-cascader>
// 递归
iterateChildren(arr, val, responseData) {
 let index = arr.map((e) => e.id).indexOf(val);
 if (index == -1) {
   arr.forEach((item) => {
     if (item.children && item.children.length) {
       index = item.children.map((e) => e.id).indexOf(val);
       if (index == -1) {
         // 该对应展开项无子集时
         this.iterateChildren(item.children, val, responseData);
       } else {
         // 该对应展开项有子集时
         this.$set(item.children, [index], {
           ...item.children[index],
           children: responseData,
         });
         if (!responseData || !responseData.length) {
           // 没有子属性时
           // 删除children属性
           delete item.children[index].children;
         }
       }
     }
   });
 } else {
  this.$set(arr, [index], {
     ...arr[index],
     children: responseData,
   });
 }
},
// 级联-展开
handleExpand(val) {
  let param = { id: val[val.length - 1] };
  areaList(param).then((res) => {
    if (res.code == "200") {
      if (res.data && res.data.length) {
        res.data.forEach((e) => {
          e.children = [];
        });
      }
      this.iterateChildren(this.areaList, val[val.length - 1], res.data);
    }
  });
},
// 级联-变化
handleAreaChange(val) {
  if (val && val.length) {
    this.infoform.areaCode = [val];
    this.areaCode = val;
  }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值