**element-ui **解决方案
<el-table
ref="multipleTable"
:data="tableData"
:row-key="getRowKeys"
@row-click="handleRowClick"
@select="handleSelectionChange"
@select-all="handleSelectAll"
>
// 注意 row-key 唯一值
<el-table-column type="selection" width="80" align="center" :reserve-selection="true">
// 注意
</el-table-column>
<el-table-column
v-for="item in columns"
:key="item.prop"
:label="item.label"
:prop="item.prop"
align="center"
></el-table-column>
</el-table>
initValue: [],
tableData: [],
selectedRowsOld: [],
selectedRows: [],
this.selectedRows = JSON.parse(JSON.stringify(this.initValue));
this.selectedRowsOld = JSON.parse(JSON.stringify(this.initValue));
getList() {
if (typeof this.request == 'function') {
this.loading = true;
this.request(this.queryParam).then(res => {
this.loading = false;
if (res.code === 200) {
this.tableData = res.data.rows;
this.pageTotal = res.data.total;
this.selectedRowsOld.forEach(rowSon => {
if (
res.data.rows.find(item => {
return rowSon.productCode == item.productCode;
})
) {
this.$refs.multipleTable.toggleRowSelection(
res.data.rows.find(item => {
return rowSon.productCode == item.productCode;
}),
true
);
}
});
} else {
this.$message.error(res.message);
}
});
}
},
handleSelectionChange(selection, row) {
const clickRow = row.productCode;
const oldSelectionIndex = this.selectedRowsOld.findIndex(item => item.productCode == clickRow);
if (oldSelectionIndex > -1) {
this.selectedRowsOld.splice(oldSelectionIndex, 1);
} else {
this.selectedRowsOld.push(row);
}
const discrepancy = this.selectedRowsOld.filter(x => !selection.some(i => i.productCode == x.productCode));
this.selectedRows = selection.concat(discrepancy);
},
handleSelectAll(selection) {
if (selection.length) {
const echoList = [];
selection.forEach(item => {
if (!this.selectedRowsOld.map(itemSon => itemSon.productCode).includes(item.productCode)) {
echoList.push(item);
}
});
this.selectedRows = this.selectedRowsOld.concat(echoList);
this.selectedRowsOld = this.selectedRows;
} else {
this.tableData.forEach(item => {
if (this.selectedRowsOld.map(itemSon => itemSon.productCode).includes(item.productCode)) {
const index = this.selectedRowsOld.findIndex(itemSon => itemSon.productCode == item.productCode);
this.selectedRowsOld.splice(index, 1);
}
});
this.selectedRows = this.selectedRowsOld;
}
},
handleRowClick(row) {
this.$refs.multipleTable.toggleRowSelection(row);
const productCode = row.productCode;
if (this.selectedRows.find(item => item.productCode == productCode)) {
const index = this.selectedRows.findIndex(item => item.productCode == productCode);
this.selectedRows.splice(index, 1);
} else {
this.selectedRows.push(row);
}
},
antDesign 解决方案
<a-table
:columns="productListColumns"
:dataSource="productData"
:rowKey="(record, index) => record.productCode"
:pagination="productPagination"
:bordered="true"
:rowSelection="{
selectedRowKeys: selectedRowKeys,
onChange: rowSelection,
onSelect: onSelect,
onSelectAll: onSelectAll
}"
@change="handleproductTable"
>
</a-table>
selectedRowKeys: [],
selectedRows: [],
rowSelection(selectedRowKeys, selectedRows) {
this.selectedRowKeys = selectedRowKeys;
},
onSelect(record, selected, selectedRows) {
if (selected) {
this.selectedRows.push(record);
}
if (!selected) {
let delIndex = this.selectedRows.findIndex(val => {
return val.id === record.id;
});
this.selectedRows.splice(delIndex, 1);
}
},
onSelectAll(selected, selectedRows, changeRows) {
if (selected) {
this.selectedRows = this.selectedRows.concat(changeRows);
}
if (!selected) {
let selectedRows = JSON.parse(JSON.stringify(this.selectedRows));
let delIndex = [];
selectedRows.forEach((item, index) => {
changeRows.forEach((val, itemIndex) => {
if (item.id === val.id) {
delIndex.push(index);
}
});
});
delIndex.forEach(item => {
delete selectedRows[item];
});
selectedRows = selectedRows.filter(item => {
return item != undefined;
});
this.selectedRows = selectedRows;
}
}