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>
相关推荐
wycode22 分钟前
Vue2实践(3)之用component做一个动态表单(二)
前端·javascript·vue.js
wycode1 小时前
Vue2实践(2)之用component做一个动态表单(一)
前端·javascript·vue.js
第七种黄昏1 小时前
Vue3 中的 ref、模板引用和 defineExpose 详解
前端·javascript·vue.js
pepedd8642 小时前
还在开发vue2老项目吗?本文带你梳理vue版本区别
前端·vue.js·trae
前端缘梦3 小时前
深入理解 Vue 中的虚拟 DOM:原理与实战价值
前端·vue.js·面试
HWL56793 小时前
pnpm(Performant npm)的安装
前端·vue.js·npm·node.js
柯南95273 小时前
Vue 3 reactive.ts 源码理解
vue.js
柯南95274 小时前
Vue 3 Ref 源码解析
vue.js
小高0074 小时前
面试官:npm run build 到底干了什么?从 package.json 到 dist 的 7 步拆解
前端·javascript·vue.js
JayceM5 小时前
Vue中v-show与v-if的区别
前端·javascript·vue.js