【VUE】表格实现自定义多选 (Ant Design Vue)

 

 

<template>
  <div class="orderManage tablePage flex_column">
    <a-table ref="tableBox_"
           :columns="columns"
           :data-source="orderListData"
            rowKey="id"
    >
      <template #customTitle>
         <span style="position: absolute;left: 10px">
           <a-checkbox v-model:checked="checkAll" @change="changeCheckAll">全选</a-checkbox>
         </span> 
          宝贝
      </template>
      <template #nameBox="rowData">
        <div class="">
            <a-checkbox :checked="selectedRowKeys.includes(rowData.text.id)" @change="changeCheck(rowData)"></a-checkbox>
            <span>复制订单号</span>
        </div>
      </template>
    </a-table>
  </div>
</template>
<script>
import { defineComponent,ref,reactive } from 'vue';
  export default defineComponent({
      name:"listTable",
      data(){
        return{
          checkAll:false, //全选状态
          selectedRowKeys: [], //选中的key
          selectedRows: [], //选中的数据
          columns:[
              {title:'单价',align:'center',width:130,customCell: (_, index) => ({colSpan: 0})},
              {title:'数量',align:'center',width:80,customCell: (_, index) => ({colSpan: 0})},
          ],
          orderListData:[{id:111,name:"网卡"},{id:222,name:"深度"},{id:333,name:"打扫"}],
        }   
  methods: {
    changeCheckAll(){
        if (this.selectedRowKeys.length === this.orderListData.length) {
          this.selectedRowKeys = [] // 判断是否已全部选中,是则清空已选列表
          this.selectedRows = [];
          this.checkAll = false;
        } else {
          this.orderListData.forEach((item) => {
            if (!this.selectedRowKeys.includes(item.id)) {
              this.selectedRowKeys.push(item.id) // 否则将未选中的全部加入已选列表中
              this.selectedRows.push(item);
            }
          })
          this.checkAll = true;
        }
    },
    changeCheck(row){
      if (!this.selectedRowKeys.includes(row.text.id)) {
        this.selectedRowKeys.push(row.text.id);
        this.selectedRows.push(row.text);
      } else {
        let index = this.selectedRowKeys.indexOf(row.text.id);
        this.selectedRowKeys.splice(index, 1);
        this.selectedRows.splice(index, 1);
      }
      console.log(" this.selectedRowKeys", this.selectedRowKeys)
      console.log(" this.selectedRows", this.selectedRows)
    },
  }
});
</script>

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Ant Design Vue中,可以使用`rowSelection`属性来实现表格多选功能。具体步骤如下: 1. 在表格组件中添加`rowSelection`属性,并设置`type`为`checkbox`,表示使用复选框来进行多选。 2. 在`rowSelection`属性中,设置`selectedRowKeys`为一个数组,用于存储当前选中的行的`key`值。 3. 在表格的列定义中,添加一列用于显示复选框,并设置`customRender`属性为一个函数,用于自定义复选框的显示和选中状态。 下面是一个示例代码: ```vue <template> <a-table :columns="columns" :data-source="dataSource" :row-selection="rowSelection"></a-table> </template> <script> export default { data() { return { dataSource: [ { id: 1, name: 'John', age: 18 }, { id: 2, name: 'Mike', age: 22 }, { id: 3, name: 'Lucy', age: 20 } ], selectedRowKeys: [] } }, computed: { rowSelection() { return { type: 'checkbox', selectedRowKeys: this.selectedRowKeys, onChange: this.handleSelectChange } }, columns() { return [ { title: 'ID', dataIndex: 'id' }, { title: 'Name', dataIndex: 'name' }, { title: 'Age', dataIndex: 'age' }, { title: 'Select', customRender: (text, record) => { const isSelected = this.selectedRowKeys.includes(record.id) return <a-checkbox checked={isSelected} onChange={() => this.handleRowSelect(record)}></a-checkbox> } } ] } }, methods: { handleRowSelect(record) { const { id } = record const index = this.selectedRowKeys.indexOf(id) if (index > -1) { this.selectedRowKeys.splice(index, 1) } else { this.selectedRowKeys.push(id) } }, handleSelectChange(selectedRowKeys) { this.selectedRowKeys = selectedRowKeys } } } </script> ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值