antd-vue - - - - - row-selection的使用

本文展示了如何在AntdVuetable中设置rowKey、rowSelection以及使用onChange和getCheckboxProps方法。文章提到了两种处理特殊需求的方法,包括通过切换CSS类隐藏选择框和在row-selection配置中动态判断是否显示选择框。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. 正常需求使用

antd-vue table
官方示例代码如下:

<template>
  <a-table :rowKey="record=>record.id" :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 === 'lee', // name为lee的行,禁止选中
        // Column configuration not to be checked
        name: record.name,
      }),
    };
    return {
      data,
      columns,
      rowSelection,
    };
  },
});
</script>

关键点如下:

  • rowKey为选中行的关键字,必须要配置!!!!!!
  • 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时即可

### a-table Selection 功能使用详解 #### 定义 Row Key 为了使 `a-table` 的选择功能正常工作,定义唯一的 `rowKey` 是必要的。这可以通过配置 `rowKey` 属性来实现,该属性可以是一个字符串或函数[^3]。 ```jsx const columns = [ { title: 'Name', dataIndex: 'name', key: 'name' }, // ... other columns ... ]; const data = [ { key: '1', name: 'John Brown' }, // ... more items ... ]; ``` #### 启用选择列 通过设置 `rowSelection` 对象启用行选择功能。此对象允许进一步定制选择行为,比如指定默认选中的行、改变勾选项的位置等[^1]。 ```jsx import React from 'react'; import { Table } from 'antd'; function App() { const rowSelection = { onChange: (selectedRowKeys, selectedRows) => { console.log(`Selected Rows: ${selectedRowKeys}`); } }; return ( <Table rowSelection={rowSelection} columns={columns} dataSource={data} rowKey="key" /> ); } export default App; ``` #### 自定义选择栏样式 如果希望隐藏默认的选择栏或者调整其外观,则可通过 CSS 来控制 `.ant-table-selection-column` 类下的元素显示状态。 ```css .ant-table-selection-column { display: none !important; /* 隐藏选择框 */ } ``` #### 处理分页场景下保持选择项一致性 当处理带有分页的数据表时,为了避免页面跳转后丢失之前的选择记录,应该保存当前页码以及所选项目的键值,在每次加载新数据前恢复这些选择。对于 Ant Design Vue 版本的 `a-table` 组件来说,可利用 `ref` 获取实例并调用相应的方法如 `toggleRowSelection` 实现这一点[^2]。 ```javascript // 假设这是在一个Vue组件内部 this.$refs.tableRef.toggleRowSelection({ [someId]: true }); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值