el-select实现下拉多选,全选,反选,清空功能

el-select实现下拉多选,全选,反选,清空功能

vue+elementui实现下拉多选,全选,反选,清空功能

显示效果 ,复制所有代码即可看到效果

在这里插入图片描述

页面组件使用

<template>
  <div style="width: 300px">
    <MultipleSelect
      ref="multipleSelect"
      :bindValue.sync="formData.stocking_status"
      :options="stockingStatusListData"
      :options-symbol="symbolListData"
      :stocking-placeholder="stockingPlaceholder"
      @changeSelect="changeSelect"
    />
  </div>
</template>

<script>
import MultipleSelect from "./components/MultipleSelect/index";
export default {
  name: "TestSelect",
  components: {
    MultipleSelect,
  },
  data() {
    return {
      formData: {
        category: "",
        stocking_status: {
          condition_code: "",
          condition_type: "",
          condition_value: "",
        },
      },
      stockingStatusListData: [
        { label: "Stocked", value: "0" },
        { label: "Unstocked", value: "1" },
        { label: "Three", value: "2" },
        { label: "Two", value: "3" },
        { label: "One", value: "4" },
      ],
      symbolListData: [
        { label: "=", value: 1, type: "IN" },
        { label: "!=", value: 2, type: "NOT IN" },
      ],
      stockingPlaceholder: "Select Stocking Status",
    };
  },

  methods: {
    changeSelect(value) {
      console.log(value, "最终值");
    },
  },
};
</script>

子组件代码 MultipleSelect

<template>
  <div class="multiple-select">
    <el-select
      slot="prepend"
      v-model="symbolSelect"
      placeholder="symbol"
      size="mini"
      clearable
      style="width: 90px"
      @change="changeSymbolSelect"
    >
      <el-option
        v-for="item in optionsSymbol"
        :key="item.value"
        :label="item.label"
        :value="item"
      />
    </el-select>

    <el-select
      v-model="stockingSelect"
      multiple
      collapse-tags
      size="mini"
      :placeholder="stockingPlaceholder"
      @change="changeStockingSelect"
      style="width: 100%"
    >
      <div class="select_up">
        <el-button type="text" @click="selectAll">
          <i class="el-icon-check" />
          All</el-button
        >
        <el-button type="text" @click="clearAll">
          <i class="el-icon-close" />
          Clear</el-button
        >
        <el-button type="text" @click="selectReverse">
          <i class="el-icon-refresh" />
          Reverse</el-button
        >
      </div>
      <div class="select_list">
        <el-option
          v-for="item in options"
          :key="item.value"
          :label="item.label"
          :value="item"
        />
      </div>
    </el-select>
  </div>
</template>

<script>
export default {
  name: "MultipleSelect",
  props: {
    // 符号下拉
    optionsSymbol: {
      type: Array,
      default: () => [],
    },
    // 下拉选项
    options: {
      type: Array,
      default: () => [],
    },
    bindValue: {
      type: Object,
      default: () => {},
    },
    // 符号下拉的placeholder
    symbolPlaceholder: {
      type: String,
      default: "symbol",
    },
    // 下拉选项的placeholder
    stockingPlaceholder: {
      type: String,
      default: "stocking_status",
    },
    // 格式化的值
    formatValue: {
      type: String,
      default: () => "label",
    },
  },
  data() {
    return {
      symbolSelect: {},
      stockingSelect: [],
      formData: {
        condition_code: "",
        condition_type: "",
        condition_value: "",
      },
    };
  },
  watch: {
    formData: {
      handler(val) {
        this.$emit("update:bindValue", val);
      },
      deep: true,
    },
  },
  methods: {
    //清空操作
    clearAll() {
      this.stockingSelect = [];
      this.formData.condition_value = "";
    },
    //全选操作
    selectAll() {
      this.stockingSelect = this.options;
      this.formatData(this.stockingSelect);
    },
    //反选操作
    selectReverse() {
      let val = this.options.filter((item) => {
        return !this.stockingSelect.includes(item);
      });
      this.stockingSelect = val;
      this.formatData(val);
    },
    // 选中值发生变化时触发
    changeStockingSelect(val) {
      this.formatData(val);
    },

    // 选中值发生变化时触发
    changeSymbolSelect(val) {
      this.stockingSelect = [];
      this.formData.condition_code = val.value;
      this.formData.condition_type = val.type;
      this.$emit("changeSelect", this.formData);
    },
    clearSymbolAndStocking() {
      this.symbolSelect = {};
      this.stockingSelect = [];
      this.formData = {
        condition_code: "",
        condition_type: "",
        condition_value: "",
      };
    },

    formatData(vals) {
      if (vals.length > 0) {
        if (this.formatValue === "label") {
          const formattedString = vals.map((item) => item.label).join(",");
          this.formData.condition_value = formattedString;
        } else {
          const formattedString = vals.map((item) => item.value).join(",");
          this.formData.condition_value = formattedString;
        }
      }
      this.$emit("changeSelect", this.formData);
    },
  },
};
</script>

<style lang="scss" scoped>
.multiple-select {
  display: flex;
}
::v-deep .el-select__tags-text {
  display: inline-block;
  max-width: 60px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.select_up {
  display: flex;
  padding: 0 12px;
  font-size: 14px;
  position: absolute;
  z-index: 9999;
  background-color: white;
  top: 0px;
  width: 100%;
  border-radius: 5px 5px 0 0;

  ::v-deep .el-button {
    color: #bcbcbc;
    font-size: 14px;

    i {
      font-size: 14px;
    }
  }
  ::v-deep .el-button:hover {
    color: #409eff;
  }

  .el-button + .el-button {
    margin-left: 6px;
  }
}
.select_list {
  margin-top: 25px;
  min-width: 180px;
}
</style>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值