elementui el-checkbox-group 自定义全选

24 篇文章 0 订阅
14 篇文章 0 订阅
        <div class="itemTime">
          <el-checkbox
            :indeterminate="isIndeterminate"
            v-model="checkAll"
            @change="handleCheckAllChange"
            >全选</el-checkbox
          >
          <el-checkbox-group
            v-model="checkedCities"
            @change="handleCheckedCitiesChange"
          >
            <el-checkbox
              v-for="city in labels"
              :label="city.department_chinese"
              :key="city.department_name"
              :value="city.department_name"
              >{{ city.department_chinese }}</el-checkbox
            >
          </el-checkbox-group>
        </div>

 

export default {
  name: "drawer",
  data() {
    return {
      labels:[{
            "department_name": "Engineering",
            "department_code": "ENG",
            "department_chinese": "工程部"
        },
        {
            "department_name": "MM",
            "department_code": "MM",
            "department_chinese": "物料管理部"
        },
        {
            "department_name": "Qua.&Envir.",
            "department_code": "QUE",
            "department_chinese": "品质与环保部"
        },
        {
            "department_name": "Production",
            "department_code": "PRO",
            "department_chinese": "生产部"
        }
    ],
      checkAll: false,
      checkedCities: [],//需要绑定的数据
      allElection: [], // 全选
      selectionArr: [], // 选中要传给后台的数据 对象
      isIndeterminate: true,
     }
   },
  mounted() {
    this.allElectionFun();//获取需要默认显示的数据
    this.DefaultFullSelection();//初始化 默认全部选中
  },
   methods: {
     allElectionFun() { // 获取需要默认显示的数据
           this.allElection = [];
             for (var i = 0; i < this.labels.length; i++) {
               this.allElection.push(this.labels[i].department_chinese)
           }
     },
    DefaultFullSelection() {
      // 初始化 默认全部选中
      this.checkedCities = this.allElection;
      let checkedCount = this.checkedCities.length;
      this.checkAll = checkedCount === this.labels.length;
      this.isIndeterminate = checkedCount > 0 && checkedCount < this.labels.length;
      this.selectionFun(this.checkedCities);
    },
    selectionFun(selectionArr) { // 获取选中的对象
              this.selectionArr = [];
               for (var i = 0; i < this.labels.length; i++) {
                 for (var j = 0; j < selectionArr.length; j++) {
                        if (selectionArr[j] === this.labels[i].department_chinese) {
                          this.selectionArr.push(this.labels[i])
                     }
                    }
              }
     },
      handleCheckAllChange(val) { // 全选
        this.allElectionFun();
         this.checkedCities = val ? this.allElection : [];
         this.isIndeterminate = false;
           // console.log(this.checkedCities);
              this.selectionFun(this.checkedCities);
             console.log(this.selectionArr)
    },
      handleCheckedCitiesChange(value) { // 选中
             let checkedCount = value.length;
             this.checkAll = checkedCount === this.labels.length;
              this.isIndeterminate = checkedCount > 0 && checkedCount < 
              this.labels.length;
               this.selectionFun(value);
             console.log(this.selectionArr)
          },
   }
}

 

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

黑色咖啡 Ken

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值