antd vue table控件的使用(二)

本文详细介绍了在AntDesignVue框架下如何配置table控件实现单选、默认选中以及获取选中项的功能,包括rowSelection属性的设置和onChange方法的使用。

今天就讲讲Ant Design Vue下的控件 ---- table表格(选择和操作)

结合项目中的需求,看看如何配置table控件,需求:

(1)根据列表中的选项,单选;

(2)根据已有的选中项,默认已选;

(3)得到被选中项的信息。

核心:rowSelection.selectedRowKeys

模板中代码如下:

<template>
  <div style="margin: 0; padding: 0">
    <a-row>
      <a-col :span="24">
        <a-table
          class="ant-table-striped"
          size="small"
          bordered
          rowKey="input_item_value"
          :data-source="brentDatas"
          :columns="brentCols"
          :pagination="false"
          :loading="loading"
          :row-class-name="(_record, index) => (index % 2 === 1 ? 'table-striped' : null)"
          :rowSelection="{
            type: 'radio',
            onChange: rowSelectedChange,
            selectedRowKeys: selectedInputItemValue,
          }">
        </a-table> 
      </a-col>
    </a-row>
  </div>
</template>

1. 设置第一列单选按钮

绑定rowSelection属性,设置type为radio;并设置rowKey为brentDatas中的主键列名

2. 设置默认选中项

绑定rowSelection属性,设置selectedRowKeys。代码如下:

const selectedInputItemValue = ref([]); //用来设置默认值,声明

selectedInputItemValue.value.push(res[0].inputItemValue);//res为选中项的结果集,将结果集第一行的inputItemValue为默认值。

3. 获取选中项,调用onChange方法

//单选事件
  function rowSelectedChange(selectedRowKeys: string[], selectRows: []) {
    selectedBrent.value = selectedRowKeys[0];
    //console.log('selectedBrent', selectedBrent.value);
    selectedInputItemValue.value = selectedRowKeys;
  }

selectedInputItemValue为获取到的选中项集合。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值