问题:回显列表默认勾选,切换分页勾选,数据出现重复或者缺失情况
解决方法:表格存在默认勾选回显,使用的是@select + @select-all方法
<template>
<div>
<el-table
v-loading="listLoading"
:data="deviceList"
size="small"
element-loading-text="正在查询中。。。"
border
fit
highlight-current-row
ref="multipleTable"
@select="onSelect"
@select-all="onSelectAll"
>
<el-table-column type="selection" width="55"> </el-table-column>
<el-table-column label="设备编号" prop="deviceId"> </el-table-column>
<el-table-column label="设备类型" prop="deviceType"> </el-table-column>
</el-table>
<div v-show="deviceTotal > 0" class="paging-box">
<el-pagination
@size-change="handleSizeChange"
@current-change="handleCurrentChange"
:current-page="deviceForm.pageNum"
:page-sizes="[20, 50, 100]"
:page-size="deviceForm.pageSize"
layout="total, sizes, prev, pager, next, jumper"
:total="deviceTotal"
>
</el-pagination>
</div>
</div>
</template>
// 获取智能设备列表
getFarmDeviceList() {
this.listLoading = true;
farmDeviceList({
pageNum: this.deviceForm.pageNum,
pageSize: this.deviceForm.pageSize,
param: {
farmTel: this.goods.farmTel,
goodsId: this.$route.query.id,
},
})
.then((res) => {
this.listLoading = false;
this.deviceList = res.data.data.records; // 获取列表数据
this.deviceTotal = res.data.data.total;
var newArr = [];
this.$nextTick(() => {
this.deviceList.forEach((item) => {
// this.confirmData 是回显勾选值
this.confirmData.forEach((ele) => {
if (item.deviceId == ele.deviceId) {
newArr.push(item);
}
});
});
newArr.forEach((item) => {
this.$refs.multipleTable.toggleRowSelection(item, true);
});
});
})
},
// 当用户手动勾选数据行的 Checkbox 时触发的事件
onSelect(selection, row) {
const index = this.confirmData.findIndex(
(i) => i.deviceId == row.deviceId
);
// 如果已经包含该项 说明本次是取消勾选
if (index > -1) {
this.confirmData.splice(index, 1);
} else {
// 否则,将已勾选的push进去
this.confirmData.push(row);
}
},
// 当用户手动勾选全选 Checkbox 时触发的事件
onSelectAll(selection) {
if (selection.length > 0) {
const deviceId = this.confirmData.map((i) => i.deviceId);
const unExist = selection.filter((i) => !deviceId.includes(i.deviceId));
this.confirmData.push(...unExist); // 新增勾选,添加新增的设备
} else {
this.deviceList.forEach((item) => {
this.confirmData.forEach((id, index) => {
if (id.deviceId == item.deviceId) {
this.confirmData.splice(index, 1); //取消勾选,删除不需要的设备
}
});
});
}
},