页面级联选择
参考elementPlus官网 Cascader 级联选择器
我变量绑定的是字符串,因为后台保存需要用字符串,所以需要数组转字符串用逗号拼接
## 代码
<el-cascader :options="statusList" v-model="ruleForm.statusArr"v-if="item.property == 'status'" placeholder="请选择状态" style="width: 100%;"clearable>
</el-cascader>
<script>
export default {
name: 'deviceInfo',
setup() {
const state = reactive({
"statusList": [
{
"value": "0",
"label": "可用",
"children": [
{
"value": "01",
"label": "开机中"
},
{
"value": "02",
"label": "关机中"
},
{
"value": "03",
"label": "其它"
}
]
},
{
"value": "1",
"label": "不可用",
"children": [
{
"value": "11",
"label": "设备停用"
},
{
"value": "12",
"label": "其它"
}
]
},
{
"value": "2",
"label": "占用",
"children": [
{
"value": "21",
"label": "计划维护保养"
},
{
"value": "22",
"label": "非计划故障送修"
},
{
"value": "23",
"label": "调试"
},
{
"value": "24",
"label": "其它"
}
]
},
{
"value": "3",
"label": "异常",
"children": [
{
"value": "31",
"label": "物料异常"
},
{
"value": "32",
"label": "质量异常"
},
{
"value": "33",
"label": "设备异常"
},
{
"value": "34",
"label": "人员异常"
},
{
"value": "35",
"label": "HSE异常"
},
{
"value": "36",
"label": "其它"
}
]
}
]
})
}
const ruleForm = reactive({
statusArr: []
})
保存的时候 statusArr转字符串
status: ruleForm.statusArr.join(','),
//查询、table渲染
function handleChange(value) {
if (value) {
state.statusArr = value
state.query.status = value.join(',')
} else {
state.query.status = []
}
}
const statusFormat = (status) => {
if (!Array.isArray(status) || status.length === 0) {
return '';
}
const labels = status.map((value) => {
const node = findNode(state.statusList, value);
return node ? node.label : '';
});
return labels.join(' - ');
};
const findNode = (list, value) => {
for (const item of list) {
if (item.value === value) {
return item;
}
if (item.children && item.children.length > 0) {
const found = findNode(item.children, value);
if (found) {
return found;
}
}
}
return null;
};
}
<script>