ant-design-vue中table组件复选框默认值设置

ant-design-vue中table组件复选框默认值设置

1、最终实现效果

2、代码示例

(1)template代码片段

<a-table
    :dataSource="dataSource"
    :columns="columns"
    :pagination="false"
    :row-selection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
    :row-key="(record) => record.key"
>
</a-table>

(2)script代码片段

export default {
	watch: {
		open(newValue) {
            // 当弹窗打开的时候,给复选框设置默认选中的数据
			if (this.chooseData.length && newValue) {
				this.selectedRowKeys = this.chooseData.map((item) => item.key)
			} else if (newValue && !this.chooseData.length) {
				this.selectedRowKeys = []
				this.pagination.current = 1
			}
		}
	},
	data() {
		return {
			dataSource: [],
			columns: [
				{
					title: '序号',
					dataIndex: 'key',
					key: 'key',
					align: 'center'
				},
				{
					title: '机构名称',
					dataIndex: 'name',
					key: 'mane',
					align: 'center'
				},
				{
					title: '机构等级',
					dataIndex: 'sex',
					key: 'sex',
					align: 'center'
				},
				{
					title: '机构性质',
					dataIndex: 'phoneNumber',
					key: 'phoneNumber',
					align: 'center'
				}
			],
			selectedRowKeys: [],
		}
	},
	methods: {
		// 复选框操作
		onSelectChange(selectedRowKeys, selectedRows) {
			this.selectedRowKeys = selectedRowKeys
		},
	}
}
ant-design-vue的树形控件,如果想要获取复选框选中的节点,可以使用以下方法: 1. 使用ref属性获取tree组件的实例,然后通过调用getCheckedKeys方法获取选中的节点的key值数组。 ```html <template> <a-tree ref="tree" :checkable="true" :tree-data="treeData"></a-tree> <button @click="getCheckedNodes">获取选中节点</button> </template> <script> export default { data() { return { treeData: [ { title: '节点1', key: '1', children: [ { title: '节点1.1', key: '1-1' }, { title: '节点1.2', key: '1-2' } ] }, { title: '节点2', key: '2', children: [ { title: '节点2.1', key: '2-1' }, { title: '节点2.2', key: '2-2' } ] } ] } }, methods: { getCheckedNodes() { const checkedKeys = this.$refs.tree.getCheckedKeys() console.log('选中的节点key值数组:', checkedKeys) } } } </script> ``` 2. 使用ref属性获取tree组件的实例,然后通过调用getCheckedNodes方法获取选中的节点的Node对象数组。 ```html <template> <a-tree ref="tree" :checkable="true" :tree-data="treeData"></a-tree> <button @click="getCheckedNodes">获取选中节点</button> </template> <script> export default { data() { return { treeData: [ { title: '节点1', key: '1', children: [ { title: '节点1.1', key: '1-1' }, { title: '节点1.2', key: '1-2' } ] }, { title: '节点2', key: '2', children: [ { title: '节点2.1', key: '2-1' }, { title: '节点2.2', key: '2-2' } ] } ] } }, methods: { getCheckedNodes() { const checkedNodes = this.$refs.tree.getCheckedNodes() console.log('选中的节点Node对象数组:', checkedNodes) } } } </script> ``` 需要注意的是,如果树形控件开启了异步加载子节点,使用getCheckedKeys方法获取选中节点时,需要等到所有节点都加载完毕才能正确获取选中节点的key值数组。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值