vue3 element plus 关于单选框/多选框回显数据

文章讲述了在Vue应用中,如何使用el-select组件与reactive状态管理配合,当回显数据时,处理数组类型的选项显示。涉及到v-model绑定、axios请求获取数据并更新select组件的内容。
摘要由CSDN通过智能技术生成

代码

<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也是数组类型,这样就回显了。

  • 7
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值