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

摘要:

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


element ui table

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

bash 复制代码
methods中声明 getRowKeys()方法 
getRowKeys(row) {
  return row.id
},

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

bash 复制代码
<el-table-column type="selection" :reserve-selection="true" width="44px" />

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

终极方法:

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

bash 复制代码
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);
           }
       })
   }
})

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

bash 复制代码
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)
		}
	})
}
相关推荐
加油乐2 分钟前
JS判断当前时间是否在指定时段内(支持多时段使用)
前端·javascript
Epat6 分钟前
关于一个小菜鸡是如何通过自定义 postcss 插件解决 color-mix 兼容问题的
前端
小小小小宇7 分钟前
webComponent实现一个拖拽组件
前端
满怀10157 分钟前
【Python核心库实战指南】从数据处理到Web开发
开发语言·前端·python
PBitW15 分钟前
工作中突然发现零宽字符串的作用了!
前端·javascript·vue.js
VeryCool16 分钟前
React Native新架构升级实战【从 0.62 到 0.72】
前端·javascript·架构
小小小小宇17 分钟前
JS匹配两数组中全相等对象
前端
xixixin_20 分钟前
【uniapp】uni.setClipboardData 方法失效 bug 解决方案
java·前端·uni-app
狂炫一碗大米饭20 分钟前
大厂一面,刨析题型,把握趋势🔭💯
前端·javascript·面试
星空寻流年26 分钟前
css3新特性第五章(web字体)
前端·css·css3