el-table多选列表已选中数据回显

el-table多选列表已选中数据回显
 <el-table :data="List" :header-cell-style="{ 'text-align': 'center' }":cell-style="{ 'text-align': 'center' }" style="width: 100%" v-loading="loading" ref="multipleTable":row-key="(row) => { return row.id }" @selection-change="handleSelectionChange">
    <el-table-column type="selection" min-width="45" :reserve-selection="true" />
     <el-table-column label="唯一id" prop="id" v-if="false" />
     <el-table-column label="名称" prop="Name" />
//目前选中信息
   ids: [],
//所有信息
  List: [],
   //已选中列表数据回显
        toggleSelectionItem() { // 在获取List后调用方法
            this.$nextTick(() => {
                this.$refs.multipleTable.clearSelection();
                this.List.forEach(row => {
                   if (row.hasAgree != 0) { //后端返回判断条件
                     this.$refs.multipleTable.toggleRowSelection(row);
                    }
                })
            })
        },
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在使用 element-plus 的 el-table 进行多选时,可以通过绑定一个对象来实现数据回显。具体实现方法如下: 1. 在获取表格数据时,将数据转化为一个以 id 为键,整个数据对象为值的字典对象。 2. 在获取回显数据时,将数据转化为一个以 deviceId 为键,整个数据对象为值的字典对象。 3. 在 el-table 的 @select 事件中,将选中数据对象存入回显数据字典对象中。 4. 在 el-table 的 @select-all 事件中,遍历所有数据对象,将其存入回显数据字典对象中。 5. 在 el-table 的 :row-selected 事件中,根据回显数据字典对象中是否存在当前数据对象的 id 或 deviceId,来判断当前数据对象是否应该被选中。 具体代码实现可以参考以下示例: ``` <template> <el-table ref="tableRef" :data="tableData" @select="handleSelect" @select-all="handleSelectAll" :row-selected="isRowSelected" row-key="id" > <el-table-column type="selection" width="55" /> <el-table-column type="index" label="序号" width="150" /> <el-table-column prop="name" label="电厂名称" /> </el-table> </template> <script> export default { data() { return { tableData: [], // 表格数据 selectedData: {}, // 回显数据 } }, methods: { // 获取表格数据 async fetchData() { const res = await fetchTableData() this.tableData = res.data.reduce((dic, item) => { dic[item.id] = item return dic }, {}) }, // 获取回显数据 async fetchSelectedData() { const res = await fetchSelectedData() this.selectedData = res.data.reduce((dic, item) => { dic[item.deviceId] = item return dic }, {}) }, // 处理单个选中事件 handleSelect(selection, row) { this.$set(this.selectedData, row.deviceId, row) }, // 处理全选事件 handleSelectAll(selection) { Object.values(this.tableData).forEach(row => { this.$set(this.selectedData, row.deviceId, row) }) }, // 判断行是否被选中 isRowSelected(row) { return !!this.selectedData[row.id || row.deviceId] }, }, mounted() { this.fetchData() this.fetchSelectedData() }, } </script> ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值