在不使用 element 内部提供的reserve-selection属性的情况下,实现跨分页多选
模板代码代码如下:
<div class="table">
<j-table ref="multipleTable" :data="tableData" :row-key="row=>row.id" @selection-change="handleSelectionChange" >
<j-table-column type="selection" width="55"></j-table-column>
<j-table-column prop="realWarehouseCode" label="仓库编码" > </j-table-column>
<j-table-column prop="realWarehouseName" label="仓库名称"> </j-table-column>
<j-table-column prop="provinceName" label="所在城市" > </j-table-column>
<j-table-column label="销售组织">
<template slot-scope="scope">{{findLabelByVal(LegalPersonList,scope.row.inventoryOrgCode,'string')}}</template>
</j-table-column>
</j-table>
</div>
接收父组件回显数据,及选中处理
props: {
selectedData: {
type: Array,
default: null
}
},
data(){
return {
tableData: [],
selectedTableData: [],
}
},
methods:{
//选中回调
handleSelectionChange (selection) {
// 当选中数组为空的时候
if (this.selectedTableData.length === 0) {
this.selectedTableData = selection
return
}
// 所有选中选项
const allItems = _.cloneDeep(this.selectedTableData)
// 当前页面数据 id集合
const currentTableData = _.cloneDeep(this.tableData).map(i => i.id)
// 非当前页选中数据
const noCurrentPageSelect = allItems.filter(i => !currentTableData.includes(i.id))
// 因为selection 仅返回当前页的所有选中数据,我们更新当前页选中数据就可以了
this.selectedTableData = [...noCurrentPageSelect, ...selection]
},
// 勾选
handleEchoSelected (currentList, allList) {
const allListIds = allList.map(i => i.id)
currentList.forEach(i => {
// 回显当前页的数据勾选
if (allListIds.includes(i.id)) {
this.$nextTick(() => {
this.$refs.multipleTable.toggleRowSelection(i, true)
})
}
})
},
}