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

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时即可

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值