element plus e-table表格中使用多选,当翻页时已选中的数据丢失

在这里插入图片描述摘要:

点击第一页选中两个,再选择第二页,选中,回到第一页,之前选中的要保留!

在这里插入图片描述
element ui table

解决办法: :row-key=“getRowKeys” (写在el-table中)

methods中声明 getRowKeys()方法 
getRowKeys(row) {
  return row.id
},

:reserve-selection=“true” (写在el-table-column中type为select的行中)

<el-table-column type="selection" :reserve-selection="true" width="44px" />

因为表格分页,点击会刷新第一页的数据的,只会加载第二页的数据,前面页面的数据是拿不到的,上面的还是不行的话加入@selection-change,当选择项发生变化时会触发该事件回调selection!

终极方法:
使用toggleRowSelection方法把数据硬塞进去

this.$nextTick(() => {
  // this.multipleSelection:第一页和第二页选中的数据数组, this.tableList是表格数据
   if (this.multipleSelection.length) {
       // this.$refs.elTable 当前表格ref
       this.$refs.elTable.clearSelection()

       this.multipleSelection.forEach(row => {
           const selectedRow = this.tableList.find(item => item.id === row.id)
           if(selectedRow){
               // 当前页(第一页)需要被选中的数据使其选中
               this.$refs.elTable.toggleRowSelection(selectedRow, true);
           }else{
               // 不在当前页(第二页)的数据,也硬塞到@selection-change的参数中,这时当你在第一页选中其他数据时,selection-change的参数也会带着第二页的数据
               this.$refs.elTable.toggleRowSelection(row, true);
           }
       })
   }
})

或者获取所有数据回来就更加好处理了!记录选中的,但是最傻了!

mounted(){
	init()
},
async init(){
	await setCheck()
	await getTableList()
},
// 这里是请求接口获取表格分页数据
getTableList(){
	// this.tableList = 
},
// 这里获取所有的数据
getAllTableList(){
	// 请求获取所有的数据,然后return出去
	// return allList
},
// 设置选中
async setCheck() {
	const allTableList = await getAllTableList()
	allTableList.forEach((item) => {
		if(ids.has(item.id)){
			this.$refs.elTable.toggleRowSelection(item, true)
		}
	})
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值