ant design vue a-table表格中插入操作按钮(以switch开关 [a-switch]为例)

1.给columns添加列属性

columns() {
 sortedInfo = sortedInfo || {};
  return [
    {
      title: "工程",
      dataIndex: "outputProject",
      width: 80
    },
    {
      title: "是否显示小数",
      dataIndex: "showDecimalsOrnot",
      width: 80,
      scopedSlots: { customRender: 'showDecimalsOrnot'}
    }
  ];
}

scopedSlots: { customRender: ‘showDecimalsOrnot’} 必须添加,目的是为了渲染HTML属性,不加画面显示不出来

2.在a-table中插入想要显示的内容

        <a-table :columns="columns" :dataSource="dataSource" :loading="loading" :pagination="false" bordered
          :rowKey="record => record.outputProject" :scroll="{ x: 100 }"
          :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange, onSelect:onSelectEvent}" 		                    v-decorator="['list',{rules: [{ required: true}]}]">
          <template slot="showDecimalsOrnot" slot-scope="text, record">
            <a-switch 
            :checked="record.showDecimalsOrnot === 1 ? true : false"
            @change="switchOnChange(record,$event)"
            :disabled ="record.outputProject === 'Appearance' ? true : false"
            />
          </template>
        </a-table>

a-switch中 slot 的属性值必须和columns中的一致【scopedSlots: { customRender: ‘showDecimalsOrnot’}】,
slot-scope中则是要传的参数

3.最终效果
在这里插入图片描述
4.联动:选中checkbox按钮勾选+switch开关同时选择时,去除前者勾选,后者自动关闭
① a-table 中添加事件:onSelect:onSelectEvent ,此处不要加传的参数,不然报错,在具体的方法中加参数如下:

     onSelectEvent(record,selected,selectedRows,event){
        if(!selected && record.showDecimalsOrnot === 1){
          record.showDecimalsOrnot = 0
        }
      }
官网:
onSelect - 用户手动选择/取消选择某列的回调 - 
Function(record, selected, selectedRows, nativeEvent)
(1)record:所传的object,
(2)selected:是否选择,
(3)selectedRows目前勾选的所有的checkbox

② 最后在a-switch 中使用三元表达式来控制是否勾选:

:checked="record.showDecimalsOrnot === 1 ? true : false"
  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值