el-select和el-checkbox实现下拉框多选及全选

现需求要求把原有的el-select下拉框变为带有el-checkbox复选框的下拉框,实现效果如图:

<el-form-item label="午饭" prop="businessTypeList">
        <el-select v-model="queryParams.businessTypeList" collapse-tags multiple placeholder="请选择" @change="handleClick()">
          <el-option :value="''">
            <el-checkbox :indeterminate="isIndeterminate" :value="checkAll" @change="handleCheckAllChange">
              全选
            </el-checkbox>
          </el-option>
          <el-option v-for="(dict,index) in businessTypeOptions"  :key="index" :label="dict.dictLabel" :value="dict.dictValue" >
            <el-checkbox style="pointer-events: none" :value="queryParams.businessTypeList.includes(dict.dictValue)" > {{ dict.dictLabel }} </el-checkbox>
          </el-option>
        </el-select>
</el-form-item>
data() {
    return {
      queryParams: this.queryParamsInit(),
      checkAll: false,
      isIndeterminate: false,
      showCheckAll: false,
      businessTypeOptions: [{
        dictLabel: '肉蟹煲',
        dictValue: '1'
      },{
        dictLabel: '烤鱼',
        dictValue: '2'
      },{
        dictLabel: '火锅',
        dictValue: '3'
      }]
    }
  },
methods:{
    // 午饭全选按钮
    handleCheckAllChange(value) {
      this.checkAll = !this.checkAll
      this.isIndeterminate = false
      if (this.checkAll) {
        this.queryParams.businessTypeList = this.businessTypeOptions.map(item => item.dictValue)
      } else {
        this.queryParams.businessTypeList = []
      }
    },
    // 午饭点击事件
    handleClick(){
      if (this.queryParams.businessTypeList.length === this.businessTypeOptions.length){
        this.checkAll = true
      }else {
        this.checkAll = false
      }
    }
}

可以实现多选,全选,取消功能,点击全选会全部选中,取消其中某选项会取消全选,不足之处请指正。

  • 5
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值