当前位置: 首页 > article >正文

vue el-select多选封装及使用

在这里插入图片描述

使用了Element UI库中的el-select和el-option组件来构建多选下拉框。同时,也包含了一个el-input组件用于过滤搜索选择项,以及el-checkbox-group和el-checkbox组件用于显示多选项。

创建组件index.vue (src/common-ui/selectMultiple/index.vue)
<template>
  <el-select
      ref="select"
      v-model="hValue"
      multiple
      collapse-tags
      :clearable="clearable"
      :disabled="disabled"
      :placeholder="placeholder"
      :popper-class="'select-default no-empty'"
      :key="poperKeyValue"
      :value-key="valueKey"
      @blur="handleBlur"
      @change="handleChange"
      @clear="handleClear"
      @focus="handleFocus"
      @remove-tag="handleRemoveTag"
      @visible-change="handleVisibleChange"
  >
    <el-input
        class="filter-input"
        v-model.trim="hFilterVal"
        v-if="filterable"
        :class="selectInfo.options.length == 0 ? 'filter-input-mb' : ''"
    ></el-input>

    <el-checkbox-group v-model="hValue">
      <el-option
          v-for="(item, index) in selectInfo.options"
          :key="index"
          :label="
          selectInfo.prop && selectInfo.prop.label
            ? item[selectInfo.prop.label]
            : item.name
        "
          :disabled="item.disabled"
          :value="
          selectInfo.prop && selectInfo.prop.value
            ? item[selectInfo.prop.value]
            : item.id
        "
      >
        <el-checkbox
            style="pointer-events: none"
            :label="
            selectInfo.prop && selectInfo.prop.value
              ? item[selectInfo.prop.value]
              : item.id
          "
        >{{
          selectInfo.prop && selectInfo.prop.label
          ? item[selectInfo.prop.label]
          : item.name
          }}
        </el-checkbox>
      </el-option>
    </el-checkbox-group>
  </el-select>
</template>

<script>
  export default {
    name: "HSelectMul",
    props: {
      placeholder: String,
      selectInfo: {
        type: Object,
        default() {
          return {
            align: "left",
            options: [],
            filterOptions: [],
            prop:{
              label:'name',
              value:'id'
            }
          };
        }
      },
      filterVal:{
        type:String,
        value:''
      },
      clearable: {
        type: Boolean,
        default: true
      },
      disabled: {
        type: Boolean,
        default: false
      },
      filterable: {
        type: Boolean,
        default: true
      },

      value: {
        type: [String, Number, Array, Boolean],
        required: true
      },
      valueKey: String,

      keyValue: {
        type: String,
        default: "select-single"
      }
    },
    data() {
      return {
        poperKeyValue: ""
      };
    },
    computed: {
      hValue: {
        get() {
          let value = [];
          if (this.value instanceof Array) {
            this.value.forEach(key => {
              if (this.checkValueExisting(key)) {
                value.push(key);
              }
            });
          }
          return value;
        },
        set(value) {
          this.$emit("input", value);
        }
      },
      hFilterVal: {
        get() {
          return this.filterVal;
        },
        set(value) {
          this.$emit("input-search", value);
        }
      },
    },
    watch: {
      keyValue(val) {
        this.poperKeyValue = val;
      }
    },
    methods: {
      checkValueExisting(value) {
        let key = this.selectInfo.prop&&this.selectInfo.prop.value?this.selectInfo.prop.value:'id'
        if(this.filterable) {
          if (this.selectInfo.filterOptions instanceof Array) {
            let index = this.selectInfo.filterOptions.findIndex(
                item => item[key] === value,
                this
            );
            return index > -1;
          }
        } else {
          if (this.selectInfo.options instanceof Array) {
            let index = this.selectInfo.options.findIndex(
                item => item[key] === value,
                this
            );
            return index > -1;
          }
        }

        return false;
      },
      handleBlur(event) {
        this.$emit("blur", event);
      },
      handleChange(value) {
        this.$emit("change", value);
      },
      handleClear() {
        this.$emit("clear");
      },
      handleFocus(event) {
        this.$emit("focus", event);
      },
      handleRemoveTag(tag) {
        this.$emit("remove-tag", tag);
      },
      handleVisibleChange(visible) {
        this.$emit("visible-change", visible);
      }
    }
  };
</script>

<style lang="scss" scoped></style>


页面引入
  • 在需要使用selectMultiple组件的地方,通过import语句组件注册并使用

<template>
  <div>
    <HSelectMultiple
        :select-info="selectInfo"
        v-model="selectedValue"
        :filter-val="filterVal"
        @input-search="dropDownSearchTop"
        @change="changeSelect"
    >
    </HSelectMultiple>
  </div>
</template>
<script>
  import HSelectMultiplefrom '@/common-ui/selectMultiple/index'

  export default {
    components: {
      HSelectMultiplefrom 
    },
    data() {
      return {
        filterVal: '',
        dataSource: [],
        selectedValue: '',
        selectInfo: {
          align: "left",
          options: [],
          filterOptions: [],
          prop: {
            label: 'name',
            value: 'id'
          }
        },
      }
    },
    methods: {
      changeSelect(val, field, parentField, info) {
        this.selectInfo.chooseSelectList = [];
        for (let i = 0; i < this.selectInfo.filterOptions.length; i++) {
          for (let j = 0; j < val.length; j++) {
            let value = this.selectInfo.prop && this.selectInfo.prop.value ? this.selectInfo.prop.value : "id";
            if (val[j] === this.selectInfo.filterOptions[i][value]) {
              this.selectInfo.chooseSelectList.push(this.selectInfo.filterOptions[i]);
            }
          }
        }

        console.log(val, '选择的值')
      },
      dropDownSearchTop(val) {
        this.filterVal = this.selectInfo.filterVal;
        if (this.selectInfo.filterVal === "") {
          this.selectInfo.options = JSON.parse(JSON.stringify(this.selectInfo.filterOptions));
          return;
        }
        let list = [];
        if (this.selectInfo.chooseSelectList.length > 0) {
          list = this.selectInfo.filterOptions.filter(item => {
            let value = ithis.selectInfonfo.prop && this.selectInfo.prop.value ? this.selectInfo.prop.value : "id";
            return this.selectInfo.chooseSelectList.every(el => el[value] != item[value]);
          });
        } else {
          list = JSON.parse(JSON.stringify(this.selectInfo.filterOptions));
        }
        this.selectInfo.options = this.selectInfo.chooseSelectList.concat(
            list.filter(item => {
              let name = this.selectInfo.prop && this.selectInfo.prop.label ? this.selectInfo.prop.label : "name";
              return item[name].includes(this.selectInfo.filterVal);
            })
        );
      },
    }
    // ...
  }
</script>

确保你已经安装了Vue.js和Element UI,并在项目中引入它们。


http://www.kler.cn/a/159426.html

相关文章:

  • 路漫漫其修远兮,吾将上下而求索---第一次使用github的过程记录和个人感受
  • 【因果分析方法】MATLAB计算Liang-Kleeman信息流
  • Kafka-Eagle的配置——kafka可视化界面
  • HarmonyOS Next星河版笔记--界面开发(5)
  • 《鸿蒙生态:开发者的机遇与挑战》
  • 自动驾驶系列—从数据采集到存储:解密自动驾驶传感器数据采集盒子的关键技术
  • 英伟达显卡驱动的相关组件和名词
  • Java安全之Commons Collections4分析
  • 虚拟人如何在线下活动实现实时交互?动捕设备或为最优解
  • 网络编程HTTP协议进化史
  • HM1111 not know C++
  • 如何进行餐饮行业研究
  • 基于c++版本的数据结构改-python栈和队列思维总结
  • Docker tag 命令
  • 工商业分布式光伏电站的盈利模式有哪些?
  • 数字工厂时代,如何实现3D数据访问与发布、WEB大模型可视化?
  • 命名实体识别
  • 虾皮关键词详情API接口在电商行业中的重要性及实时数据获取实现
  • DNS协议(DNS规范、DNS报文、DNS智能选路)
  • 【Python】zip
  • 树_二叉搜索树小绝对差
  • C# Demo--汉字转拼音
  • Java中常用的垃圾回收器
  • 软件工程 复习笔记
  • Vue混淆与还原
  • python处理的例子