【vue组件】element下拉框全选

效果

          <el-select
            v-model="entity.entityTypeIds"
            :placeholder="$t('0omr1DmkT5yr5CRm7fWBd')"
            filterable
            multiple
            collapse-tags
            ref="serviceSelect"
            @change="searchSelectEntityTypeIds"
            class="mix-select-style"
          >
            <i slot="prefix" class="el-input__icon el-icon-search"></i>
            <el-option
              v-if="entityTypes.length > 0"
              :class="[
                { all: isIndeterminate === 'all' },
                { part: isIndeterminate === 'part' },
                { no: isIndeterminate === 'no' }
              ]"
              :label="$t('selectAll')"
              value="选项0"
            >
            </el-option>
            <el-option
              :title="item.text"
              v-for="item in entityTypes"
              :key="item.id"
              :label="item.text"
              :value="Number(item.id)"
            >
              {{ item.text }}
            </el-option>
          </el-select>
    searchSelectEntityTypeIds (val) {
      let allValues = []
      for (let item of this.entityTypes) {
        allValues.push(item.id)
      }
      allValues = ['选项0'].concat(allValues)
      const oldVal = this.oldSelectArray.concat([])
      const oldInclude0 = oldVal.includes('选项0')
      const newInclude0 = val.includes('选项0')
      // 若是全部选择
      if (newInclude0) {
        this.entity.entityTypeIds = allValues
        this.isIndeterminate = 'all'
      }
      if (oldInclude0 && !newInclude0) {
        this.entity.entityTypeIds = []
        this.isIndeterminate = 'no'
      }
      if (oldInclude0 && newInclude0) {
        this.entity.entityTypeIds = val
        this.entity.entityTypeIds = this.entity.entityTypeIds.filter(
          d => d !== '选项0'
        )
        this.isIndeterminate = 'part'
      }
      if (!oldInclude0 && !newInclude0) {
        if (val.length === allValues.length - 1) {
          this.entity.entityTypeIds = ['选项0'].concat(val)
          this.isIndeterminate = 'all'
        } else if (!val.length) {
          this.isIndeterminate = ''
        } else {
          this.isIndeterminate = 'part'
        }
      }
      this.oldSelectArray = this.entity.entityTypeIds
    },
<style lang="stylus">
.el-select-dropdown.is-multiple .all.selected:after {
  content: '\e6da';
}

.el-select-dropdown.is-multiple .part:after {
  position: absolute;
  content: '\e6d8';
  right: 20px;
  font-family: element-icons;
  font-size: 12px;
  font-weight: 700;
  color: #5374c0;
}

.el-select-dropdown.is-multiple .no.selected:after {
  content: "''";
}

.indexDrawer .el-drawer__wrapper{
  width: 66% !important;
  left: auto !important;
}
</style>

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值