vue2element实现全选反选

根据业务需求 需要实现一个全选反选 样式如下图

用select 加Checkbox 多选框实现样式

<template>
  <el-select v-model="dataForm.checked"  multiple >
    <el-checkbox class="checkS" :indeterminate="isIndeterminate" v-model="checkAll" @change="handleCheckAllChange">全选</el-checkbox>
     <el-option v-for="item in cities" :key="item.id" :label="item.label" :value="item.id">
     <el-checkbox :value="dataForm.checked.includes(item.id)" :label="item.label" :key="item.id" @change="(val)=>secleChange(val,item)">{{ item.label }}</el-checkbox>
    </el-option>
    </el-select>
</template>
<script>
export default {
  data() {
    return {
      list: [
        { id: 1, name: '选项A' },
        { id: 2, name: '选项B' },
        { id: 3, name: '选项C' },
        // ...更多选项
      ],
      checkedList: [], // 已选项
      checkAll: false, // 全选框状态
      isIndeterminate: false, // 不确定状态
    };
  },
  watch: {
   dataForm:{
        handler(newVal, oldVal) {
        console.log('Message changed from', oldVal, 'to', newVal);
        if(newVal.checked.length > 0 && newVal.checked.length!= this.cities.length){
          this.isIndeterminate = true
        }else if(newVal.checked.length > 0 && newVal.checked.length == this.cities.lengt){
          this.checkAll = true
        }else if(!newVal.checked.length){
          this.checkAll = false
          this.isIndeterminate = false
        }
      },
        deep: true, // 深度监听msg对象内部属性的变化
        immediate: true // 组件初始化时立即执行handler函数
      }
  },
  methods: {
   // 全选
      handleCheckAllChange(val) {
        if(val){
          // 选中
          this.dataForm.checked = this.cities.map(i =>{ return i.id})
        }else{
          // 未选中
          this.dataForm.checked = []
        }
     },
   secleChange(val,row){
        // 选择框单个多选
        console.log(val,row)
          if(val){
            // 取消
            let newarr =  this.dataForm.checked.filter((k,index) => {
              if(val){
                return k != row.id
              }
            })
            return this.$set(this.dataForm,'checked',newarr)
          }else{
            // 新增
            const newArry = [...this.dataForm.checked,row.id]
            return this.$set(this.dataForm,'checked',newArry)
          }     
      },
     // 默认全选(在mounted调用)
      initSelect(){
        if(this.secondaryList){
          this.dataForm.customerIdList = this.secondaryList.map(i =>{ return i.id})
          this.checkAll = true
        }
      },
  },
};
</script>
<style lang="scss" scoped>
 .checkS{
    padding:0 20px;
  }
  /deep/.el-select-dropdown.is-multiple,.el-select-dropdown__item.selected::after {
    display:none !important;
  }
</style>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值