ant-desin-vue——table全选时自定义的禁用行也被选上,且最后一行不选中问题
效果
错误(序号1是获取数据后,初始化禁用的项): 正确:

原因
未给数据指定key
代码
<template>
<a-table :row-selection="rowSelection" :columns="columns" :data-source="data">
<a slot="name" slot-scope="text">{{ text }}</a>
</a-table>
</template>
<script>
const columns = [
{
title: 'Name',
dataIndex: 'name',
scopedSlots: { customRender: 'name' },
},
{
title: 'Age',
dataIndex: 'age',
},
{
title: 'Address',
dataIndex: 'address',
},
];
const data = [
{
id: 1,
name: 'John Brown',
age: 32,
address: 'New York No. 1 Lake Park',
},
{
id: 2,
name: 'Jim Green',
age: 42,
address: 'London No. 1 Lake Park',
},
{
id: 3,
name: 'Joe Black',
age: 32,
address: 'Sidney No. 1 Lake Park',
},
{
id: 4,
name: 'Disabled User',
age: 99,
address: 'Sidney No. 1 Lake Park',
},
];
export default {
data() {
return {
data,
columns,
selectedRowKeys:[],//选中的key,清空选中时赋值空数组即可
selectId: [], // 选中的id
};
},
computed: {
rowSelection() {
return {
selectedRowKeys: this.selectedRowKeys,
onChange: (selectedRowKeys, selectedRows) => {
let that = this;
that.selectedRowKeys = selectedRowKeys;
let arr = selectedRows.map(k => k.id);
that.selectId = [...arr];
},
getCheckboxProps: record => ({
props: {
disabled: record.name === 'Disabled User', // 禁用项
name: record.name,
},
}),
};
}, },
mounted(){
let that = this; for (let i in that.data) { that.data[i].key = parseInt(i) + 1; // <== 关键——全选时不选择禁用数据 } } }; </script>

浙公网安备 33010602011771号