3.5.0 版本及以上
row-config=“{ isCurrent: true, keyField: ‘id’ }”
checkbox-config=“{ checkRowKeys: selectRowsId, reserve: true }”
<template>
<div>
<vxe-table
ref="table"
resizable
:data="tableData1"
:row-config="{ isCurrent: true, keyField: 'id' }"
:checkbox-config="{ checkRowKeys: selectRowsId, reserve: true }"
@checkbox-change="changeBox"
@checkbox-all="changeBoxAll"
>
<vxe-column type="checkbox" title="ID" width="280"></vxe-column>
<vxe-column field="name" title="Name"></vxe-column>
</vxe-table>
<vxe-pager
align="left"
:current-page.sync="page1.currentPage"
:page-size.sync="page1.pageSize"
:total="page1.totalResult"
@page-change="pageChange"
>
</vxe-pager>
<el-button @click="selectExceed">选择Id大于3的行</el-button>
<el-button @click="selectBelow">选择ID小于3的行</el-button>
<el-button @click="clearAll">清除所有已勾选数据</el-button>
</div>
</template>
<script> export default {
data() {
return {
tableData1: [],
page1: {
currentPage: 1,
pageSize: 5,
totalResult: 10,
},
selectList: [],
};
},
computed: {
selectRowsId() {
return this.selectList.map((item) => item.id);
},
},
created() {},
mounted() {
this.handlerSearch();
},
methods: {
// 搜索
handlerSearch(currentPage, pageSize) {
this.page1.currentPage = Number(currentPage) ? Number(currentPage) : 1;
this.page1.pageSize = Number(pageSize) ? Number(pageSize) : 5;
this.queryTable();
},
// 分页
pageChange({ currentPage, pageSize }) {
this.handlerSearch(currentPage, pageSize);
},
// 获取数据
async queryTable() {
const { data } = await this.$axios("/api/list", {
params: {
currentPage: this.page1.currentPage,
pageSize: this.page1.pageSize,
},
});
if (data?.code === 0) this.tableData1 = data.list;
},
// 单个选择
changeBox({ records }) {
this.handlerSelectRow(records);
},
// 全选
changeBoxAll({ records }) {
this.handlerSelectRow(records);
},
// 处理表格中选择的行!!!!!
handlerSelectRow(current = []) {
const tab = this.$refs.table;
const reserves = tab && tab.getCheckboxReserveRecords();
const result = reserves.concat(current);
console.log('选择的rows',result);
},
// 选择大于3的ID
selectExceed(){
const tab = this.$refs.table;
let row = this.tableData1.filter(item => item.id > 3)
tab && tab.setCheckboxRow(row, true);
this.handlerSelectRow(row)
},
// 选择小于3的ID
selectBelow(){
const tab = this.$refs.table;
let row = this.tableData1.filter(item => item.id < 3)
tab && tab.setCheckboxRow(row, true);
this.handlerSelectRow(row)
},
// 清除所有选项
clearAll(){
const tab = this.$refs.table
tab && tab.clearCheckboxReserve()
tab && tab.clearCheckboxRow()
this.selectList = []
}
},
}; </script>
<style lang="less" scoped>
</style>