级联选择器el-cascader处理复杂数据(四层、五层数据),回显部门以及部门下的人员

注意:参考第五层的数据处理比较nice

当级联选择器需要绑定的数组不再是简易数据,props涉及的字段不再是一个,而是列表里面套列表

比如,我想要获取部门以及下面的员工,如何显示?如下图1所示,后端返回的数据,sysDepartmentList是部门分组,sysUserList是员工分组,可是最终要在级联上呈现如下图2所示,怎么做到?好在公司的前一个项目有相关例子,足够我参考完成,记录下来,希望能帮到跟我一样的小白

图1:
在这里插入图片描述

图2(四层数据)
在这里插入图片描述
处理数据:

getOtherUsername(){
	//首先调用接口获取数据
      this.$axios.get('pm-security/sysUser/getDepartmentUser').then(res=>{
        console.log(res.data.data);//图1所示
        let resData = res.data.data;
        resData[0].sysUserList = resData[0].sysDepartmentList;
        resData[0].sysUserList.forEach((item, index) => {
          if (item.sysDepartmentList){
            item.sysUserList = item.sysDepartmentList;
            item.sysUserList.forEach((childItem, childIndex) => {
              if (childItem.sysUserList) {
                childItem.sysUserList.forEach((childItem2, childIndex) => {
                  childItem2.departmentName = childItem2.name;
                })
              }
            })
          }else {
            if (item.sysUserList) {
              item.sysUserList.forEach((childItem1, childIndex) => {
                childItem1.departmentName = childItem1.name;
              })
              return
            }
          }
        })
        this.cascaderOptions = resData;//cascaderOptions 是级联绑定的数组
      })
    },

补充:后来考虑到公司有分公司的情况,就加了一层数据

图3(五层数据)
在这里插入图片描述
处理数据:

getOtherUsername(){
      this.$axios.get('pm-security/sysUser/getDepartmentUser').then(res=>{
        let resData = res.data.data;
        console.log(resData)
        //共5层
        //第一层部门信息
        resData[0].sysUserList = resData[0].sysDepartmentList;
        resData[0].sysUserList.forEach((item, index) => { 
          //第二层有部门信息
          if (item.sysDepartmentList){
            item.sysUserList = item.sysDepartmentList;
            item.sysUserList.forEach((item2, item2Index) => { 
              //第三层有部门信息
              if(item2.sysDepartmentList){
                item2.sysUserList=item2.sysDepartmentList;
                item2.sysUserList.forEach((item3,item3Index)=>{
                  //第四层
                  if (item3.sysUserList) {
                    item3.sysUserList.forEach((item4, childIndex) => {
                      item4.departmentName = item4.name;
                      this.userInfo.push(item4)
                    })
                  }else {
                    item3.disabled = true;//部门下没有人员,禁止选择
                  }
                })
              }else {
                //第三层没有部门信息,直接取人员
                if (item2.sysUserList) {
                  item2.sysUserList.forEach((item5, childIndex) => {
                    item5.departmentName = item5.name;
                    this.userInfo.push(item5)
                  })
                }else {
                  item2.disabled = true;//部门下没有人员,禁止选择
                }
              }
            })
          }else {
            //第二层没有部门信息,直接取人员
            if (item.sysUserList) {
              item.sysUserList.forEach((item6, childIndex) => { //item6 第二层人员
                item6.departmentName = item6.name;
                this.userInfo.push(item6)
              })
              return
            }
          }
        })
        this.cascaderOptions = resData;
      })
    },
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于 Element UI 的级选择 el-cascader,如果需要回显已经选中的值,可以通过设置 v-model 和 :options 属性来实现。 假设有一个三级动的级选择,选项数据如下: ``` options: [{ value: 'zhinan', label: '指南', children: [{ value: 'shejiyuanze', label: '设计原则', children: [{ value: 'yizhi', label: '一致' }, { value: 'fankui', label: '反馈' }] }, { value: 'daohang', label: '导航', children: [{ value: 'cexiangdaohang', label: '侧向导航' }, { value: 'dingbudaohang', label: '顶部导航' }] }] }] ``` 如果需要回显已经选中的值,可以将当前选中的值通过 v-model 绑定到 data 中的一个变量上,例如: ``` <el-cascader v-model="selectedOptions" :options="options" ></el-cascader> ``` 其中,selectedOptions 是一个数组,用于保存当前选中的值。 接下来,需要在 mounted 钩子函数中设置已选中的值,例如: ``` mounted() { this.selectedOptions = ['zhinan', 'shejiyuanze', 'yizhi'] } ``` 这样,在页面加载完成后,级选择就会自动回显已经选中的值。 需要注意的是,如果级选择的选项数据是异步加载的,需要在加载完成后再设置已选中的值,例如: ``` mounted() { this.loadOptions().then(() => { this.selectedOptions = ['zhinan', 'shejiyuanze', 'yizhi'] }) }, methods: { loadOptions() { // 异步加载选项数据 } } ``` 这样就可以实现 Element UI 的级选择 el-cascader回显功能了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值