使用Ant Design of Vue中的Checkbox 多选框实现多组全选和全不选

1、实现效果图如下:

在这里插入图片描述

2、代码实现如下(采用vue+antd):

template:

<template>
  <!--多个区全选弹窗-->
  <div class='sale_allot'>
    <a-modal
      :maskClosable='false'
      class='sale_allot'
      title="多个全选弹窗"
      v-model="visible1"
      :bodyStyle="{width:'100%'}"
      height='500px'
    >
      <div style="min-height:300px;">
        <div class='item_check' v-for='(item,index) in totalRolesList' :key='index'>
          <div>
            <div :style="{ borderBottom: '1px solid #E9E9E9',marginTop:'15px'}">
              <a-checkbox :indeterminate="item.indeterminate" :checked="item.checkAll" @change="onCheckAllChange($event,index)">
                {{ item.orgName }}
              </a-checkbox>
            </div>
            <br />
            <a-checkbox-group  v-model="item.checkedList"  :options="item.userList" @change="onChange($event,index)" >
             <span slot='label' slot-scope='{value}'>
                 {{value}}
             </span>
            </a-checkbox-group>
          </div>
        </div>
      </div>
      <template slot="footer">
        <a-button key="back" @click="noOk">
          暂不选择
        </a-button>
        <a-button key="submit" type="primary" :loading="loading" @click="onOk">
          确定选择
        </a-button>
      </template>
    </a-modal>
  </div>
</template>

script部分:

<script>
//多选框的数据
const listData = [
  {
    orgName: "火影村",
    checkAll:false,
    indeterminate : false,
    checkedList : [],
    userList: [
      {
        label: "鸣人",
        value:1
      },
      {
        label: "佐助",
        value: 2
      },
      {
        label: "自来也",
        value: 3
      },
      {
        label: "卡卡西",
        value: 4
      },
      {
        label: "小樱",
        value: 5
      }
    ]
  },
  {
    orgName: "晓组织",
    checkAll:false,
    indeterminate : false,
    checkedList : [],
    userList: [
      {
        label: "佩恩",
        value:1
      },
      {
        label: "角度",
        value: 2
      },
      {
        label: "长门",
        value: 3
      },
      {
        label: "蝎",
        value: 4
      },
      {
        label: "再不斩",
        value: 5
      }
    ]
  }
]
export default {
  props:{
    selectedRowKeys:{
      type:Array,  //类型
      require:false,  //必传
      default:()=> []
    }
  },
  data() {
    return {
      visible1:false,
      totalRolesList:listData,//所有组员角色数据
      loading:false,//确定分配的按钮的响应状态
    }
  },
  methods: {
    onChange(e,index) {//单个选择
      this.totalRolesList[index].indeterminate = !!this.totalRolesList[index].checkedList.length && this.totalRolesList[index].checkedList.length < this.totalRolesList[index].userList.length;
      this.totalRolesList[index].checkAll = this.totalRolesList[index].checkedList.length === this.totalRolesList[index].userList.length;
    },
    onCheckAllChange(e,index) {//选择全部的事件
      //如果选中全选的话就遍历拿出当前相对项的数组的全部值
      let newArr = []
      if(e.target.checked){
        newArr = this.totalRolesList[index].userList.map(item=>{
          return item.value
        })
      }
      this.totalRolesList[index].checkedList = e.target.checked ? newArr:[]
      this.totalRolesList[index].indeterminate = false
      this.totalRolesList[index].checkAll = e.target.checked
    },
    //确定选择
    onOk(){
      //将所有选中的人员的id拿出来拼接到一个数组里面
      let userIds = []
      let sendData = {}
      this.totalRolesList.forEach(item=>{
        userIds = [...userIds,...item.checkedList]
      })
    },
    //暂不选择
    noOk(){
      this.loading=false
      this.visible1 = false
    }
  }
}
</script>

style部分:

<style lang='less'>
.sale_allot.ant-modal{
  width: 700px!important;
}
.sale_allot .ant-modal-body{
  width: 100%;
  overflow: auto;
  max-height: 550px;
}
</style>
Ant Design Vue2 表格,可以通过设置 `row-selection` 属性来为表格添加复选框。如果需要为复选框全选处添加文字,可以在 `row-selection` 属性设置 `checkStrictly` 和 `renderCell` 两个属性。具体做法如下: 1. 在 Table 组件设置 `row-selection` 属性,其 `checkStrictly` 属性用于控制是否严格检查复选框的选状态,`renderCell` 属性用于渲染复选框列,并在全选处添加文字。 ``` <template> <a-table :columns="columns" :data-source="data" row-selection="{checkStrictly: true, renderCell: renderSelection}"> </a-table> </template> ``` 2. 在 `methods` 定义 `renderSelection` 方法,用于渲染复选框列和全选处文字。在 `renderSelection` 方法,先根据 `record` 和 `index` 判断当前行是否可选,然后根据 `record` 的 `selected` 属性确定复选框的选状态,最后在全选处添加文字。 ``` <script> export default { data() { return { columns: [ { title: '', dataIndex: 'checkbox', width: '50px' }, { title: 'Name', dataIndex: 'name', key: 'name' }, { title: 'Age', dataIndex: 'age', key: 'age' } ], data: [ { id: '1', name: 'John Brown', age: 32, selected: false }, { id: '2', name: 'Jim Green', age: 42, selected: false }, { id: '3', name: 'Joe Black', age: 32, selected: false } ] }; }, methods: { renderSelection(h, { record, index }) { const selectable = !record.disabled; const selected = record.selected; return ( <div class="selection-cell"> <a-checkbox value={record.id} disabled={!selectable} checked={selected} on-change={(e) => this.handleSelectionChange(e, record)} ></a-checkbox> {index === 0 && ( <span class="selection-all"> {this.isAllSelected() ? '取消全选' : '全选'} </span> )} </div> ); }, isAllSelected() { return this.data.every((item) => item.selected); }, handleSelectionChange(e, record) { record.selected = e.target.checked; } } }; </script> <style scoped> .selection-cell { display: flex; align-items: center; } .selection-all { margin-left: 10px; color: #1890ff; cursor: pointer; } </style> ``` 这样就可以为表格的复选框全选处添加文字了。需要注意的是,如果需要获取选的行,可以通过 `this.data.filter(item => item.selected)` 来获取。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值