一个特殊下拉选择

复制代码
<template>
  <el-select
    id="selectInput"
    ref="searchSelect"
    v-model="num"
    filterable
    placeholder="请选择"
    :filter-method="dataFilter"
    @visible-change="visibleChange"
    @focus="onFocus"
    @blur="onBlur"
    @change="onChange"
  >
    <el-option
      v-for="item in options"
      :key="item.account"
      :label="item.name"
      :value="item.account"
    >{{ item.name }} </el-option>
  </el-select>
</template>

<script>
export default {
  name: 'KeepSelectValue',
  model: {
    prop: 'value',
    event: 'change',
  },
  props: {
    jsonData: {
      type: Array,
      default: () => [],
    },
    wants: {
      type: String,
      default: 'id',
    },
    value: {
      type: [String, Number],
      default: "",
    },
  },
  data() {
    return {
      options: this.jsonData,
      num: this.value,
      optionsFilter: this.jsonData,
    };
  },
  watch: {
    value: {
      handler(val) {
        this.num = val;
      },
      immediate: true,
      deep: true,
    },
  },

  methods: {
    dataFilter(val) {
      this.num = val;
      if (val) {
        this.options = this.optionsFilter.filter((item) => {
          if (item.name.includes(val) || item.name.toUpperCase().includes(val.toUpperCase())) {
            return true;
          }
        });
      } else {
        this.options = this.optionsFilter;
      }
    },
    onFocus(e) {
      this.options = this.jsonData;
      const value = e.target.value;
      setTimeout(() => {
        const input = this.$refs.searchSelect.$children[0].$refs.input;
        input.value = value;
      });
    },
    onBlur() {
      console.log(this.num, 'sssss');
      this.$emit("update:value", this.num);
      this.$emit("change", this.num);
    },
    onChange(val) {
      this.$emit("update:value", val);
      this.$emit("change", val);
    },
    visibleChange(val) {
      if (!val) {
        const input = this.$refs.searchSelect.$children[0].$refs.input;
        input.blur();
      }
    },
  },
};
</script>
复制代码

 

基于element 的el-select。主要功能是当输入值不与下拉数据匹配时,将该输入值获取。本样例客制化严重。如要封装到自己框架的话,请用心修改。element-ui本身也提供了类似的方法,(filterable,default-first-option)两个属性。看需求取用吧

posted @   前端小菜鸡美哥  阅读(21)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~
点击右上角即可分享
微信分享提示