一个特殊下拉选择
<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)两个属性。看需求取用吧
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~