el-table复选框实现分页记忆

el-table 结合 el-pagination 实现分页后默认是没有复选框记忆功能的,想到采用Map数据结构来存储所选选项。但是vue响应数据是不支持的,MapSet里面的元素变化时Vue追踪不到这些变化,因此无法做出响应。

解决方法是将Map可序列化,写在computed中实现响应式。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
  data () {
return {
myMap:new Map(),
}
},
methods:{
selectMemoriedDataFn () { // 分页记忆自动选中方法
let currentArr = this.myMap.get(this.currentPage) // 当前分页对应被选中数据
console.log('选中',currentArr)
if (currentArr && currentArr.length) { // 存在则继续执行
let tempRowsIDs = this.tableData.map(L => L.id) // 当前分页被选中数据的id集合
currentArr.forEach((item) => { // 遍历当前分页被选中数据
if (tempRowsIDs.includes(item.id)) { // id匹配上,则选中
this.$refs.multipleTable.toggleRowSelection(this.tableData[tempRowsIDs.indexOf(item.id)])
}
})
}
},
// 选中选项触发
handleSelect(val){
this.myMap.set(this.currentPage,val)
let all=[]
for (let value of this.myMap.values()) {
value.forEach(item=>{
all.push(item)
this.multipleSelectionAll=all
}
)
}
},
// currentPage 改变时会触发
handleCurrentChange(val){
this.currentPage=val
if(this.myMap.has(this.currentPage)){
this.flag=true
this.selectMemoriedDataFn()
console.log('flag change',this.flag)
}
this.getData()
},
},
computed:{
// 序列化map
mySetAsList() {
// var x = this.mySetChangeTracker;
// By using `mySetChangeTracker` we tell Vue that this property depends on it,
// so it gets re-evaluated whenever `mySetChangeTracker` changes
return Array.from(this.myMap);
}
},
watch:{
tableData () { // 监听数据变化,然后执行分页记忆自动选中回调
this.$nextTick(() => {
this.selectMemoriedDataFn()
})
}
},

踩坑:async-validator [inputvalue is required]

调单验证规则失效

没有绑定form表单导致校验失败

参考文章:https://blog.csdn.net/SGAFPZys/article/details/80754786

查看评论