element UI-实现table分页回显和数据收集

15 篇文章 0 订阅

1)table添加

:row-key

@selection-change

2)多选列添加

:reserve-selection="true"

3)data中添加相关变量

4)method中添加相关方法

@selection-change="handleSelectionChange"
handleSelectionChange (ary) {
      console.log(ary)
      this.dataSure = ary
      // this.multipleSelection = val
      console.log('this.dataSure;;;;', this.dataSure)
      this.select_order_number = this.dataSure.length
      console.log('this.select_order_number;;;', this.select_order_number)
      this.select_orderId = []
      if (ary) {
        ary.forEach(row => {
          if (row) {
            this.select_orderId.push(row.contNo)
          }
        })
      }
    }

实现效果,表格分页时,会保留每页的选中状态及将所有的选中数据放在一个数组中

5)清除数组
如果想清除数组,

table上需要添加refs

然后调用

clearSelection()

 

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值