el-checkbox-group的全选与反选

需求如下:

思路:在点击全部时按钮组双向绑定赋值全部值,点击按钮组内按钮计算选中按钮数量与按钮组数量对比,判定是否选中全部

代码如下:

javascript 复制代码
<template>
  <div>
    <el-checkbox-button v-model="checkall" @change="handleCheckAllChange">全部</el-checkbox-button>
    <el-checkbox-group v-model="state" @change="checkRadio"  >
      <el-checkbox-button v-for="city in cities" :label="city" :key="city">{{city}}</el-checkbox-button>
    </el-checkbox-group>
  </div>
 
</template>
<script>
  const cityOptions = ['上海', '北京', '广州', '深圳'];
  export default {
    data () {
      return {
        cities: cityOptions,
        checkall:false,
        state:[]

      };
    },
    methods:{
        handleCheckAllChange(val){
            this.state=val?this.cities:[]
            this.getList()
        },
        checkRadio(val){
            this.state=val
            const checkedCount=val.length
            this.checkAll=checkedCount===this.cities.length
            this.getList()
        },
        getList(){

        }
     }
  }
</script>
相关推荐
pas13621 小时前
36-mini-vue nextTick
前端·javascript·vue.js
VX:Fegn089521 小时前
计算机毕业设计|基于springboot + vue教务管理系统(源码+数据库+文档)
vue.js·spring boot·课程设计
梅梅绵绵冰21 小时前
springboot初步1
java·前端·spring boot
星辰_mya21 小时前
nginx之待续-没写完
前端
Irene199121 小时前
Vue3中 <slot >不支持 ref 属性的替代方案
vue.js·ref
GISer_Jing21 小时前
大语言模型Agent入门指南
前端·数据库·人工智能
多仔ヾ21 小时前
Vue.js 前端开发实战之 10-网络请求和 UI 组件库
vue.js
运筹vivo@1 天前
BUUCTF: [极客大挑战 2019]Upload
前端·web安全·php·ctf
qq_12498707531 天前
基于Spring Boot的长春美食推荐管理系统的设计与实现(源码+论文+部署+安装)
java·前端·spring boot·后端·毕业设计·美食·计算机毕业设计
运筹vivo@1 天前
攻防世界: easyupload
前端·web安全·php·ctf