级联选择器 数据转换及回显

1.封装组件将Json数据转换成级联数据

export function setTree(list, parentId = 0) {
    const arr = [];
    list.forEach((item) => {
        if (item.parentId === parentId) {
            const children = setTree(list, item.id);
            if (children.length > 0) {
                item.children = children;
            }
            arr.push(item);
        }
    });
    return arr;
}

 2.只有最后一级id,反查出父级id

 //回显
    findId(array, id, arr) {
      for (let i = 0; i < array.length; i++) {
        if (array[i].id === id) {
          arr.unshift(array[i].id); 
          this.addData.reportingUnit = arr;
          return arr;
        } else if (Array.isArray(array[i].children)) {
          const result = this.findId(array[i].children, id, arr);
          if (result) {
            arr.unshift(array[i].id); 
            this.addData.reportingUnit = arr;
            return result;
          }
        }
      }
      return null;
    },

3.

 //报备单位下拉列表
    getReportingUnit() {
      const id = this.$store.getters.userInfo.deptId;
      this.$api.affair.getDept({ region: this.$store.getters.userInfo.dept.region })
        .then((res) => {
          this.array = setTree(res.data, res.data[0].parentId);

          this.findId(this.array, id, this.list);
        });
    },

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值