element UI的Cascader 级联选择器在树形列表中编辑使用

今天一个设计是在树形列表中编辑表单,其中有Cascader 级联选择器 级联选择器的选择结果一般都是数组,而后端参数只要最后一级的id,假如只传最后一级id,那回显可能就会有问题;

解决办法是:多传一个参数,

参数A:1(后端要的id);

参数B:[1,2,3,4],选择后的完整数组(自己使用,用于回显),
需要注意的是传给后端的时候需要转换成字符串,回来的时候再转换成数组使用

代码如下:

html 复制代码
<el-cascader v-model="row.responsibleDeptPath" :options="deptList" :props="treeOption"></el-cascader>
javascript 复制代码
<script>
    export default {
        data (){
            return {
                deptList: [],
                treeOption: {
                   checkStrictly: true,
                   label: 'title',
                   value: 'id',
                },       
            }
        },
        created(){
            const aa = [
    {
        responsibleDept: 1892533,
        responsibleDeptPath: "[\"1837608\",\"1837616\",\"1892533\"]",
        andonAbnormalTypeVOList: [
            {
                responsibleDept: 1892533,
                responsibleDeptPath: "[\"1837608\",\"1837616\",\"1892533\"]",
                andonAbnormalTypeVOList: [
                    {
                        responsibleDept: 1892533,
                        responsibleDeptPath: "[\"1837608\",\"1837616\",\"1892533\"]",
                    }
                ]
            }
        ]
    }
];

// 遍历树形结构并转换 responsibleDeptPath 为数组
const convertDeptPathToArray = (data) => {
    return data.map(item => {
        // 将 responsibleDeptPath 字符串解析为数组
        item.responsibleDeptPath = JSON.parse(item.responsibleDeptPath);

        // 如果有子项,递归处理
        if (item.andonAbnormalTypeVOList && item.andonAbnormalTypeVOList.length > 0) {
            item.andonAbnormalTypeVOList = convertDeptPathToArray(item.andonAbnormalTypeVOList);
        }

        return item;
    });
};

const result = convertDeptPathToArray(aa);
console.log(result);
        }
    }
</script>
相关推荐
whltaoin11 分钟前
【浏览器CORS问题解决方案】SpringBoot+Vue3前后端全覆盖:浏览器跨域问题的多样化解决方案
vue.js·spring boot·浏览器跨域问题
QuantumLeap丶2 小时前
《uni-app跨平台开发完全指南》- 04 - 页面布局与样式基础
vue.js·微信小程序·uni-app
掘金安东尼2 小时前
GPT-6 会带来科学革命?奥特曼最新设想:AI CEO、便宜医疗与全新计算机
前端·vue.js·github
洞窝技术2 小时前
前端人必看的 node_modules 瘦身秘籍:从臃肿到轻盈,Umi 项目依赖优化实战
前端·vue.js·react.js
啷咯哩咯啷3 小时前
el-table-v2 实现自适应列宽
前端·javascript·vue.js
景早4 小时前
小黑记账清单案例(axios,echarts,vue)
前端·vue.js·echarts
Java 码农4 小时前
vue 使用vueCli 搭建vue2.x开发环境,并且指定ts 和less
前端·vue.js·less
计算机学姐5 小时前
基于SpringBoot的公务员考试管理系统【题库组卷+考试练习】
java·vue.js·spring boot·后端·java-ee·intellij-idea·mybatis
逆风优雅5 小时前
vue 实现自定义message 全局提示
前端·javascript·vue.js
fruge8 小时前
Vue项目中的Electron桌面应用开发实践指南
前端·vue.js·electron