Vue - Element 选择器 el-select 既可以选择下拉又可以手动输入文本功能(手动输入的值只能是数字 并且支持4位小数)

Vue - Element 选择器 el-select 既可以选择下拉又可以手动输入文本功能(手动输入的值只能是数字 并且支持4位小数)

备注

filterable 下拉框开启快速搜索功能

no-match-text 当输入的内容在下拉框中找不到时;下拉框提示的文字

@handFocus 触发输入框

@change 点击下拉选项

@visible-change下拉框出现/隐藏时触发

@keyup.native 输入搜索内容实时触发(在此方法中对输入格式做限制)

template部分

bash 复制代码
 <el-select
    v-model="min"
    filterable
    placeholder="最小值"
    no-match-text="可手动输入"
    style="width:90px;margin-right: 4px;"
    ref="inputSelectMin" 
    @focus="handFocus" 
    @change="handChange"
    @visible-change="handleVisibleChange"
    @keyup.native="inputData"
>
    <el-option
        v-for="item in minToleranceTypeList"
        :key="item.codeValue"
        :label="item.name"
        :value="item.codeValue" 
     />
</el-select>

逻辑部分( el-select 实现 既可以输入内容又可以快速选择)

bash 复制代码
  private isSelectChange:boolean= false;// 判断是选择下拉选项 默认未选择
  handChange (data:any) {
    this.min = data
    this.isSelectChange = true;
  }
  handleVisibleChange(val:any){
    if(!val){
      let input = this.$refs[ref]?.$children[0].$refs.input;
      input.blur();
    }
  }
  handFocus(){
    let input = this.$refs.inputSelectMin?.$children[0].$refs.input;
    input.blur= ()=> {
      if (!this.isSelectChange) {
        this.min = input.value;
      }
      this.isSelectChange = false
    }
  }
  // 输入框只能输入数字,最大可支持4位小数
  inputData(val:any){
    let strData = val.srcElement.value
    if (/^(\.)/.test(strData)) {
      // 匹配第一个字符是否为 .
      val.srcElement.value = strData.substring(
        strData.lastIndexOf(".") + 1,
        strData.length
      );
      return;
    }
    if (/[^\d.]+/.test(strData)) {
      // 匹配中间是否插入了字母,等其他字符
      val.srcElement.value = strData.replace(/[^\d.]+/, "");
      return;
    }
    if (/([0-9]\d*)(\.\d*){2,}/.test(strData)) {
      // 匹配是否有多个 . --恶意输入
      val.srcElement.value = "";
      return;
    }
    if (/^([0-9]\d{0,7}(\.\d{5,}))$/.test(strData)) {
      // 小数点后是否超过了3位-恶意输入
      val.srcElement.value = strData.substring(0, strData.lastIndexOf(".") + 5);
      return;
    }
    val.srcElement.value = strData.substring(0, strData.length);
  }
相关推荐
小满zs2 小时前
React-router v7 第五章(路由懒加载)
前端·react.js
Aotman_2 小时前
Vue el-from的el-form-item v-for循环表单如何校验rules(二)
前端·javascript·vue.js
BillKu3 小时前
Vue3父子组件数据双向绑定示例
javascript·vue.js·elementui
在无清风4 小时前
Java实现Redis
前端·windows·bootstrap
_一条咸鱼_5 小时前
Vue 配置模块深度剖析(十一)
前端·javascript·面试
yechaoa6 小时前
Widget开发实践指南
android·前端
赤橙红的黄6 小时前
Spring Boot中接入DeepSeek的流式输出
java·服务器·javascript
前端切图仔0016 小时前
WebSocket 技术详解
前端·网络·websocket·网络协议
upp7 小时前
[bug]langchain agent报错Invalid Format: Missing ‘Action Input:‘ after ‘Action:‘
javascript·python·langchain·bug
JarvanMo7 小时前
关于Flutter架构的小小探讨
前端·flutter