elementUI的翻页,全选只能选中当前页,不能翻页选中,同时,回显也不能翻页回显
采用如下方式实现翻页回显、单选、全选:
vue代码
<el-table
:data="tableData"
class="demo-table-thead thead theme-table-thead"
size="small"
style=" line-height: 24px;"
ref="refTable"
@select="handleSelect"
@select-all="handleSelectAll"
:selection="selectarr"
:row-key="(row)=>row.groupId"
v-loading="loading">
<el-table-column
type="selection"
width="55"
align="center"
:reserve-selection="true">
</el-table-column>
</el-table>
列表查询——回显
//查询,表格回显
search (page) {
const params = {
page: this.pagination.current,
pageSize: this.pagination.size,
}
if (page) {
params.page = page
}
this.loading = true
getist(params)
.then((response) => {
this.loading = false
this.tableData = response.data.records ? response.data.records : []
//获取表格分页数据之和,初始为[],用于在翻页选中的时候判断
this.allData = this.allData.concat(this.tableData)
this.pagination.total = Number(response.data.total)
// table回显
this.$nextTick(() => {
this.tableData.forEach((row, index) => {
if (this.selectarr.findIndex((v) => v.groupId === row.groupId) >= 0) {
this.$refs.refTable.toggleRowSelection(this.$refs.refTable.data[index], true)
}
})
})
})
.catch(() => {
this.loading = false
})
},
如果toggleRowSelection不生效,可以在外边包裹setTimeout即可
setTimeout(() => {
this.$nextTick(() => {
this.selectComs.map(row => {
this.$refs.poolTableRef.toggleRowSelection(this.poolTable.find(item => row.id === item.id), true)
})
})
}, 10)
js代码:单选,全选方法。(selectarr为选中的数组,或者需要回显是外部传入的数组)
// 单选方法
handleSelect (selection, newV) {
if (selection.find(item => item.groupId === newV.groupId)) { //选中
this.selectarr.push(newV)
} else { //取消选中
this.selectarr = this.selectarr.filter(item => item.groupId !== newV.groupId)
}
this.$emit('selected', this.selectarr)
},
// 全选方法
handleSelectAll (rows) {
if (rows && rows.length > 0) { //全选或(当前页取消全选+前一页有选中)
rows.forEach(row => {
if (!this.selectarr.find(item => item.groupId === row.groupId)) { //不在当前选中里
this.selectarr.push(row)
}
})
//上一页有选中,当前页取消全选
this.selectarr = this.selectarr.filter(item => rows.find(val => val.groupId === item.groupId))
} else { //取消全选
this.selectarr = this.selectarr.filter(item => !this.allData.find(val => item.groupId === val.groupId))
}
this.$emit('selected', this.selectarr)
},