代码
<el-form-item label="分类名称" prop="depIds">
<el-select v-model="backInfo.category" placeholder="选择分类" style="width: 240px">
<el-option v-for="item in backInfo.categoryList" :key="item.value" :label="item.label" :value="item.value" />
</el-select>
</el-form-item>
<el-form-item label="适合部门" prop="depIds">
<el-select v-model="backInfo.depIds" multiple placeholder="下拉多选框,可搜索" style="width: 240px" filterable>
<el-option v-for="item in backInfo.depDataSourceList" :key="item.value" :label="item.label" :value="item.value" />
</el-select>
</el-form-item>
const backInfo = reactive({
id: "",
name: "",
cateId: "",
categoryList: [{}],
depIds: [],
depDataSourceList: [{}],
startTime: "",
endTime: "",
des: "",
funds: 0.0,
difficulty: 0,
status: 0,
createName: "",
background: "",
content: "",
achievementsAndProspects: "",
remarks: "",
files: []
});
const updateInfo = (row: { id: "" }) => {
dialogFormVisible.value = true;
//回显数据
baseService.get(`/rp/projectinfo/getInfoById/${row.id}`).then((res) => {
Object.assign(backInfo, res.data);
backInfo.difficulty = Number(res.data.difficulty);
backInfo.status = Number(res.data.status);
});
//回显分类
baseService.get(`/sys/dept/findByIndexTwo`).then((res) => {
// 初始化 desList 数组为空
backInfo.depDataSourceList.length = 0;
// 使用 push 方法向 desList 数组中添加元素
for (let index = 0; index < res.data.length; index++) {
backInfo.depDataSourceList.push({
id: res.data[index].id,
value: res.data[index].id,
label: res.data[index].name
});
}
});
//回显分类
baseService.get(`rp/projectcate/list`).then((res) => {
backInfo.categoryList.length = 0;
for (let index = 0; index < res.data.length; index++) {
backInfo.categoryList.push({
id: res.data[index].id,
value: res.data[index].id,
label: res.data[index].name
});
}
});
};
总结
回显的时候,el-option 框内的待选数据如果是数组类型,那么el-select也是数组类型,这样就回显了。