a-table selection 默认选中问题

默认选中不生效相关代码 html

<a-table
      v-if="dataSource && dataSource.length>0 || show"
      class="h-table"
      :loading="tableLoading"
      :dataSource="dataSource"
      :defaultExpandAllRows="true"
      :columns="tableColumn"
      :scroll="scroll"
      :pagination="false"
      tableLayout="fixed"
      @resizeColumn="handleResizeColumn"
      :row-selection="isSelect ? rowSelection : null" 
    >
    
    </a-table>

vue script

  const rowSelection = reactive({
    checkStrictly: props.checkStrictly,
    selectedRowKeys:props.selectedRowKeys,
    onChange: (selectedRowKeys: any, selectedRows: any) => {
      emits('batch', selectedRowKeys, selectedRows);
    },
    getCheckboxProps: (record: any) => ({
        disabled: record.name === 'disabled',
        name: record.name,
        defaultChecked: true
    })
  })

组件是经过二次封装的,需要传入的props.selectedRowKeys为默认选中,selectedRowKeys,defaultChecked 这两个属性是和选中相关的,无论怎么改都不生效

猜测是因为selectedRowKeys发生变化时,getCheckboxProps,selectedRowKeys相关属性没有动态变化,监听属性变化,重新设置后生效

 watch(()=>props.selectedRowKeys,val=>{
    rowSelection.selectedRowKeys = val
    rowSelection.checkStrictly = props.checkStrictly,
    
    rowSelection.getCheckboxProps = (record: any) => ({
        disabled: record.name === 'disabled',
        name: record.name,
        defaultChecked: val?.includes(record.id)
    })
  })

这样默认选中生效了,但是又有问题,选择框无法使用了

selectedRowKeys 是表格选中数据,如果数据写死,选择框相当于绑定固定数据,需要配合onChange使用,触发选中时修改值

  watch(()=>props.selectedRowKeys,val=>{
    rowSelection.selectedRowKeys = val
    rowSelection.checkStrictly = props.checkStrictly,
    rowSelection.onChange = (selectedRowKeys: any, selectedRows: any) => {
      rowSelection.selectedRowKeys = [...selectedRowKeys]
      emits('batch', selectedRowKeys, selectedRows);
    }
    rowSelection.getCheckboxProps = (record: any) => ({
        disabled: record.name === 'disabled',
        name: record.name,
        defaultChecked: val?.includes(record.id)
    })
  })

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值