1. 正常需求使用
antd-vue table
官方示例代码如下:
<template><a-table :row-selection="rowSelection" :columns="columns" :data-source="data"><template #bodyCell="{ column, text }"><template v-if="column.dataIndex === 'name'"><a>{{ text }}</a></template></template></a-table>
</template>
<script>
import { defineComponent } from 'vue';
const columns = [{title: 'Name',dataIndex: 'name',
}, {title: 'Age',dataIndex: 'age',
}, {title: 'Address',dataIndex: 'address',
}];
const data = [{key: '1',name: 'John Brown',age: 32,address: 'New York No. 1 Lake Park',
}, {key: '2',name: 'Jim Green',age: 42,address: 'London No. 1 Lake Park',
}, {key: '3',name: 'Joe Black',age: 32,address: 'Sidney No. 1 Lake Park',
}, {key: '4',name: 'Disabled User',age: 99,address: 'Sidney No. 1 Lake Park',
}];
export default defineComponent({setup() {const rowSelection = {onChange: (selectedRowKeys, selectedRows) => {console.log(`selectedRowKeys: ${selectedRowKeys}`, 'selectedRows: ', selectedRows);},getCheckboxProps: record => ({disabled: record.name === 'Disabled User',// Column configuration not to be checkedname: record.name,}),};return {data,columns,rowSelection,};},
});
</script>
关键点如下:
- table标签上配置
row-selection
配置项 - 在
row-selection
配置项内需包含onChange
&getCheckboxProps
2. 特殊需求使用
需要动态切换是否展示table的选择框
2.1 治标办法
动态切换class,样式如下
.ant-table-selection-column {display: none;
}
2.2 治本办法
在配置row-selection
时多一步判断,如:
:row-selection="inner ? rowSelection : undefined"
当将其值设为undefined时即可