el-table 结合 el-pagination 实现分页后默认是没有复选框记忆功能的,想到采用Map数据结构来存储所选选项。但是vue响应数据是不支持的,Map
与Set
里面的元素变化时Vue追踪不到这些变化,因此无法做出响应。
解决方法是将Map可序列化,写在computed中实现响应式。
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