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>
相关推荐
niucloud-admin8 小时前
web 端前端
前端
摘星编程11 小时前
React Native for OpenHarmony 实战:Linking 链接处理详解
javascript·react native·react.js
胖者是谁11 小时前
EasyPlayerPro的使用方法
前端·javascript·css
EndingCoder11 小时前
索引类型和 keyof 操作符
linux·运维·前端·javascript·ubuntu·typescript
liux352811 小时前
Web集群管理实战指南:从架构到运维
运维·前端·架构
沛沛老爹11 小时前
Web转AI架构篇 Agent Skills vs MCP:工具箱与标准接口的本质区别
java·开发语言·前端·人工智能·架构·企业开发
摘星编程12 小时前
React Native for OpenHarmony 实战:ImageBackground 背景图片详解
javascript·react native·react.js
小光学长12 小时前
基于Web的长江游轮公共服务系统j225o57w(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
前端·数据库
摘星编程13 小时前
React Native for OpenHarmony 实战:Alert 警告提示详解
javascript·react native·react.js
Joe55613 小时前
vue2 + antDesign 下拉框限制只能选择2个
服务器·前端·javascript