分页选中思路:
- 渲染table数据,添加一个空数组(checkList),存储选择的数据;
- table 添加 @select 事件,返回 selection 和 row;
- 如果数组中没有该项,则将选中的项(row)添加进数组中,反之则在数组中删除该项数据;
handleSelectionChange(selection, row) {
for (let i in this.checkList) {
if (row.commentid == this.checkList[i].commentid) {
this.checkList.splice(i, 1)
console.log(this.checkList)
return false
}
}
this.checkList.push(row)
},
- 在渲染 table 数据的地方,创建一个新的空数组(arr),将 table 数据在 checkList 数组中的项全部查出来,添加到新数组(arr)中;
提供一种思路:将 checkList 中的特殊项,例如 id ,拼接成字符串
然后用 for 循环遍历出table中每项数据的 id 是否在该字符串中,将符合项添加进空数组中;
let arr = []
let list = ''
if (that.checkList.length > 0) {
for (let i in that.checkList) {
list = list + that.checkList[i].commentid + ','
}
}
for (let i in this.table) {
if (list.indexOf(res.data[i].commentid) > -1) {
arr.push(res.data[i])
}
}
- 最后,将查找出来的选中数组渲染到table中
that.$nextTick().then(function () {
arr.forEach(row => {
that.$refs.lecturerTable.toggleRowSelection(row);
});
})
最后渲染选中的字段放在 table 的 @select 事件中无效,自测只能从数据中重新查找出来才能生效
*默认选中可直接按第四步和第五步操作;
*全选可直接在table中的 @select-all 事件选择的数据替换成 checkList 即可;