el-select增加全选选项

效果截图:

在这里插入图片描述

在这里插入图片描述

<template>
  <div>
    <span>专业:</span>
    <el-select
      multiple
      collapse-tags
      @change="specialityNoChange"
      :value="specialityNoList"
    >
      <el-option :key="-999" label="全选" :value="-999"></el-option>
      <el-option
        v-for="item in specialityList"
        :key="item.specialityNo"
        :label="item.specialityName"
        :value="item.specialityNo"
      >
      </el-option>
    </el-select>
  </div>
</template>
<script>
export default {
  data() {
    return {
      selectQueryList: {
        specialityNoList: [],
      },
      specialityNoList: [],
      specialityList: [
        {
          specialityName: "建筑",
          specialityNo: "ZY0001",
        },
        {
          specialityName: "总图",
          specialityNo: "SZY0046",
        },
        {
          specialityName: "结构",
          specialityNo: "ZY0003",
        },
        {
          specialityName: "给排水",
          specialityNo: "ZY0004",
        },
        {
          specialityName: "暖通",
          specialityNo: "ZY0006",
        },
        {
          specialityName: "燃气",
          specialityNo: "SZY0005",
        },
        {
          specialityName: "电气",
          specialityNo: "ZY0005",
        },
        {
          specialityName: "弱电",
          specialityNo: "SZY0003",
        },
        {
          specialityName: "消防电",
          specialityNo: "SZY0045",
        },
        {
          specialityName: "智能化",
          specialityNo: "SZY0004",
        },
        {
          specialityName: "BIM",
          specialityNo: "ZY0011",
        },
        {
          specialityName: "土石方工程",
          specialityNo: "ZY0030",
        },
      ],
    };
  },
  methods: {
    // 筛选专业改变
    specialityNoChange(val) {
      const list = this.specialityList;
      const idList = list.map((el) => el.specialityNo);
      if (val.indexOf(-999) >= 0) {
        if (
          this.specialityNoList.indexOf(-999) >= 0 &&
          val.length === list.length
        ) {
          val.splice(0, 1);
          this.specialityNoList = val;
        } else {
          this.specialityNoList = [-999, ...idList];
        }
      } else {
        if (val.length === list.length) {
          if (this.specialityNoList.indexOf(-999) >= 0) {
            this.specialityNoList = [];
          } else {
            this.specialityNoList = [-999, ...val];
          }
        } else {
          this.specialityNoList = val;
        }
      }
      this.selectQueryList.specialityNoList = this.specialityNoList.filter(
        (e) => e != -999
      );
      this.$nextTick(() => {
        console.log(this.selectQueryList);
      });
    },
  },
};
</script>

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值