1、需求:
1.一个带选择框的具有分页功能的表格
2.根据后端返回的数据回显已勾选的数据
3.已勾选的数据不会因为分页而丢失
4.收集勾选中的数据向后端发送请求
2、效果展示

3、html代码部分
<el-table
:data="tableData"
ref="product"
style="width: 100%"
stripe
:header-cell-style="headClass"
:cell-style="rowClass"
v-loading="tableLoading"
:row-key="getId"
@select="handleSelectionChange"
@select-all="handleAllChange"
>
<el-table-column
type="selection"
width="52"
:reserve-selection="true"
></el-table-column>
<el-table-column
prop="name"
label="用户姓名"
min-width="14%"
show-overflow-tooltip
></el-table-column>
<el-table-column
prop="street"
label="所属街道"
min-width="14%"
show-overflow-tooltip
></el-table-column>
<el-table-column
prop="community"
label="所属社区"
min-width="14%"
show-overflow-tooltip
></el-table-column>
<el-table-column
prop="mobile"
label="联系电话"
min-width="14%"
show-overflow-tooltip
></el-table-column>
<el-table-column prop="status" label="用户状态" min-width="14%">
<template v-slot="{ row }">
<div v-if="row.status == 1">启用</div>
<div v-else>禁用</div>
</template>
</el-table-column>
</el-table>
//这是封装的分页器
<div class="pagination-box">
<Paging
:initTotal="total"
@sizeChange="handleSizeChange"
@pageChange="handleCurrentChange"
></Paging>
</div>
在el-table标签上添加:row-key属性
:row-key="getId"这是唯一标识符的作用
el-table-column type="selection"上添加 :reserve-selection="true"
:reserve-selection="true":行数据的 Key,用来优化 Table 的渲染;在使用 reserve-selection 功能与显示树形数据时,该属性是必填的。类型为 String 时,支持多层访问:user.info.id,但不支持 user.info[0].id,此种情况请使用 Function(elementUI的解释)。
4、script代码部分
<script>
import userAPI from "@/api/other_user/user.api.js";
import Paging from "@/components/Paging";
export default {
components: { Paging },
data() {
return {
tableLoading: false,
total: 0,
currentUserId: 0,
parentId: 0,
tableData: [],
echoList: [],
selectUserIds: [],
queryParam: {
pageNumber: 1,
pageSize: 10,
},
};
},
methods: {
openDialog(row) {
this.currentUserId = row.id;
this.linkVisible = true;
this.pageByGridAssociated();
this.Page();
},
Page() {
userAPI.pageByGrid(this.queryParam).then(async (res) => {
this.tableData = await res.list;
this.total = res.total;
this.$nextTick(() => {
this.tableData.forEach((i) => {
if (this.echoList.includes(i.id)) {
this.$refs.product.toggleRowSelection(i, true);
}
});
});
});
},
pageByGridAssociated() {
userAPI.pageByGridAssociated({ id: this.currentUserId }).then((res) => {
this.echoList = res.list.map((item) => item.id);
this.selectUserIds = this.echoList;
});
},
handleSelectionChange(selected, row) {
if (!this.echoList.includes(row.id)) {
this.echoList.push(row.id);
} else {
this.echoList.forEach((id, index) => {
if (id === row.id) {
this.echoList.splice(index, 1);
}
});
}
this.selectUserIds = this.echoList;
},
handleAllChange(selected) {
if (selected.length > 0) {
selected.forEach((item) => {
if (!this.echoList.includes(item.id)) {
this.echoList.push(item.id);
}
});
} else {
this.tableData.forEach((item) => {
this.echoList.forEach((id, index) => {
if (id === item.id) {
this.echoList.splice(index, 1);
}
});
});
}
this.selectUserIds = this.echoList;
},
getId(row) {
return row.id;
},
closeDialog() {
this.queryParam = {
pageNumber: 1,
pageSize: 10,
};
this.$refs.product.clearSelection();
this.linkVisible = false;
},
handleClose(down) {
this.$refs.product.clearSelection();
down();
},
submit() {
let param = {
dataSource: process.env.VUE_APP_DATA_SOURCE_HCS,
currentUserId: this.currentUserId,
selectUserIds: this.selectUserIds,
};
userAPI.userRoleBind(param).then((res) => {
this.$message.success("操作成功");
this.closeDialog();
});
},
handleSizeChange(page) {
this.queryParam.pageNumber = 1;
this.queryParam.pageSize = page.pageSize;
this.Page();
},
handleCurrentChange(page) {
this.queryParam.pageNumber = page.pageNumber;
this.Page();
},
headClass() {
return "background: #DFECFF;font-size:.175rem;font-weight:600;color:#333;";
},
rowClass() {
return "font-size: .175rem;color: #333333;";
},
},
};
</script>
5.题外话
单项勾选以及取消勾选:
单项勾选操作方法:
handleSelectionChange,当点击checkbox时,循环判断echoList中是否有当前行信息,
若是没有,则说明是选中状态,push进echoList中;
若是存在当前行信息,则说明是取消勾选,则将echoList中当前行id删除。
全选以及取消全选:
handleAllChange,全选方法有一个参数selecteds,这是一个选中项数组,记录了有多少条选中项;
当进行全选操作时,判断selecteds数组的length,若length>0,
循环selecteds判断echoList数组中是否与selecteds中id匹配,
若不匹配,则表示新增选中项,循环判断是为了去除重复项
关于只在第一页操作选中与取消勾选,
而第二页有默认选中的数据,
提交数据的时候会不会只提交当前页面的数据而丢失其他数据的问题,
经过测试目前是不存在的,当然是不排除其他特殊情况的(目前没发现)