1.HTLM 添加按钮全选,要有一个全选标识状态
<el-button
type="success"
@click="selectAll"
>
{{ sign === 1 ? '全 选' : '取 消 全 选' }}
</el-button>
Table
<el-table
v-loading="loading"
ref="accessControlTable"
:data="tableData"
:max-height="tableHeight"
:span-method="arraySpanMethod"
header-cell-class-name="custom-table"
@selection-change="handleSelectionChange"
@select="selectExceptCardIds"
@select-all="selecetAll"
>
<el-table-column
type="selection"
width="30"
/>
<el-table-column
prop="id"
type="index"
align="center"
width="50"
label="序号"/>
...
</el-table>
2.JS相关代码如下:
// 数据
data() {
return {
tableData: [],
sign: 1, // 是否点击全选,1未点击全选,2,点击全选
checkedArr: [], // 勾选的行数组
uncheckedArr: [], // 取消勾选的行数组
}
表格数据获取接口返回值要做如下操作
// 切换分页及分页条数全选
this.sign === 2 && this.chooseAllPages()
this.sign === 1 && this.checkedSelected()
相关操作方法如下:
// 选中事件
handleSelectionChange(val) {
console.log('多选id===handleSelectionChange', val)
},
// 合并表格最后三列
arraySpanMethod({ row, column, rowIndex, columnIndex }) {
if (columnIndex === 6) {
return [0, 0]
} else if (columnIndex === 5) {
return [1, 2]
}
},
// 全选切换
selectAll() {
this.sign = this.sign === 1 ? 2 : 1
console.log('sign', this.sign)
this.chooseAllPages()
this.checkedArr = []
this.uncheckedArr = []
},
// 全选所有页面
chooseAllPages() {
if (this.sign === 2) {
// 全选
this.$nextTick(() => {
this.tableData.forEach(row => {
// 没有取消过的勾选
if (this.uncheckedArr.map(v => v.id).indexOf(row.id) < 0) {
this.$refs.accessControlTable.toggleRowSelection(row, true)
}
})
})
} else {
// 取消全选
this.$nextTick(() => {
this.$refs.accessControlTable.clearSelection()
})
}
},
// 切换分页时选择之前选中
checkedSelected() {
console.log('切换分页时选择之前选中')
this.$nextTick(() => {
if (this.checkedArr.length === 0) return
this.tableData.forEach(row => {
if (this.checkedArr.map(v => v.id).indexOf(row.id) >= 0) {
this.$refs.accessControlTable.toggleRowSelection(row, true)
}
})
})
},
// 全选后取消单个选择事件,当用户手动勾选数据行的 Checkbox 时触发的事件
selectExceptCardIds(selection, row) {
// selection 当前页面所有选中的项
// row 当前点击操作的项
console.log('全选后取消单个选择事件,当用户手动勾选数据行的 Checkbox 时触发的事件', selection, row)
// 所有页面不全选
if (this.sign === 1) {
if (selection.indexOf(row) >= 0) {
// 新增(勾选上)
selection.map(el => {
if (this.checkedArr.map(v => v.id).indexOf(el.id) === -1) {
this.checkedArr.push(el)
}
})
console.log('所有选中的', this.checkedArr)
} else {
// 取消勾选
this.checkedArr.map((el2, index) => {
if (el2.id === row.id) {
this.checkedArr.splice(index, 1)
}
})
console.log('删除后选中的', this.checkedArr)
}
} else {
// 所有页面全选
if (selection.indexOf(row) >= 0) {
// 重新勾选上
this.uncheckedArr.map((el, index) => {
if (el.id === row.id) {
this.uncheckedArr.splice(index, 1)
}
})
} else {
// 取消勾选
this.uncheckedArr.push(row)
}
console.log('剔除的id集合', this.uncheckedArr)
}
},
// 当前页面全选
selecetAll(selection) {
console.log('当前页面全选', selection)
if (this.sign === 1) {
// 不是全选按钮状态下-考虑选中哪些
if (selection.length > 0) {
// 选中
selection.map(row => {
if (this.checkedArr.map(v => v.id).indexOf(row.id) < 0) {
this.checkedArr.push(row)
}
})
} else {
// 取消选中
this.tableData.map(row => {
this.checkedArr.map((el2, index) => {
if (el2.id === row.id) {
this.checkedArr.splice(index, 1)
}
})
})
}
console.log('不是全选按钮状态下-考虑选中哪些', this.checkedArr)
} else {
// 全选按钮状态下-考虑不选中哪里
if (selection.length === 0) {
this.tableData.map(row => {
this.uncheckedArr.push(row)
})
} else {
selection.map(row => {
this.uncheckedArr.map((el, index) => {
if (el.id === row.id) {
this.uncheckedArr.splice(index, 1)
}
})
})
}
console.log('全选按钮状态下-考虑不选中哪里', this.uncheckedArr)
}
},
备注:其中id是每条记录的唯一标识,当全选按钮点击后,按钮变成取消全选,切换标识sign的值,接口传参带入全选标识和剔除行id数组uncheckedArr,若是默认状态非全选,则带入非全选标识和选中行id数组checkedArr。