分页选中
<el-table :row-key="(row: any) => row.id">
<el-table-column type="selection" :reserve-selection="true"></el-table-column>
</el-table>
El-table 的全部属性
<el-table
:data="tableData"
ref="tableRef"
border
stripe
:row-key="(row: any) => row.id"
@select="handleSelectRow"
@select-all="handleSelectAll"
show-overflow-tooltip
>
******
</el-table>
获取选中数据
单选:判断单选是选中还是取消选中
const handleSelectRow = (selection: any, row: any) => {
let selected = selection.includes(row)
if (selected) {
checkoutList.value.push(row)
} else {
checkoutList.value = checkoutList.value.filter((item: {id: number}) => item.id !== row.id)
}
}
多选 :判断单选是选中还是取消选中
const handleSelectAll = (selection: any) => {
if (getIsAllChecked()) {
checkoutList.value = [...checkoutList.value, ...selection]
} else {
let tableDataIds = tableData.value.map((item: {id: number}) => item.id)
checkoutList.value = checkoutList.value.filter((item: {id: number}) => !tableDataIds.includes(item.id))
}
}
const getIsAllChecked = () => {
return tableRef.value.store.states.isAllSelected.value
}
反显数据
tableData.value.forEach((item: any) => {
if (checkoutIds.value?.includes(item.id)) {
tableRef.value.toggleRowSelection(item, true)
}
})