首页 > 其他分享 >Vue中实现输入框的自动补全功能

Vue中实现输入框的自动补全功能

时间:2024-04-09 10:59:40浏览次数:12  
标签:Vue 补全 item 列表 输入框 filterInputCompleteList data

一、百度文心一言的结果

在Vue中实现输入框的自动补全功能,可以使用原生的input事件监听用户的输入,并通过计算属性或方法来提供匹配的自动补全建议。以下是一个简单的例子:

<template>
  <div>
    <input type="text" v-model="userInput" @input="updateSuggestions">
    <ul v-if="suggestions.length">
      <li v-for="(suggestion, index) in suggestions" :key="index" @click="selectSuggestion(suggestion)">{{ suggestion }}</li>
    </ul>
  </div>
</template>

<script>
export default {
  data() {
    return {
      userInput: '',
      suggestions: [],
      // 这里是一个示例的自动补全数据源
      dataSource: ['apple', 'banana', 'cherry', 'date', 'fig', 'grape']
    };
  },
  methods: {
    updateSuggestions() {
      // 使用filter方法根据用户输入过滤数据源
      this.suggestions = this.dataSource.filter(item => item.startsWith(this.userInput));
    },
    selectSuggestion(suggestion) {
      this.userInput = suggestion;
      this.suggestions = []; // 选择之后清空建议列表
    }
  }
};
</script>

<style>
ul {
  list-style: none;
  padding: 0;
  margin: 0;
}

li {
  cursor: pointer;
  padding: 5px;
  background: #f0f0f0;
  border-bottom: 1px solid #eee;
}

li:hover {
  background: #e9e9e9;
}
</style>

在这个例子中,我们有一个输入框和一个下拉列表用于显示自动补全的建议。当用户输入时,updateSuggestions 方法会被触发,计算匹配用户输入的补全建议,并更新suggestions数组。当用户点击列表中的项时,selectSuggestion 方法会被触发,用户的输入会被设置为所选择的补全建议,并清空建议列表。

二、项目实战

输入框和下拉框

<el-row type="flex" class="row-bg">
          <el-col :span="12">
            <el-form-item label="地区" prop="area">
              <el-input v-model="ruleForm.area" placeholder="请输入地区" class="input1" style="width: 240px;" @input="autoComplete"></el-input>
            </el-form-item>
          </el-col>
</el-row>
<div class="input_complete" :style="inputCompleteStyle" v-if="inputComplete">
   <div v-for="item in filterInputCompleteList" :key="item.id" class="complete_data" @click="selectInputComlete(item)">{{item.chiShortName}}</div>
</div>

数据模型

filterInputCompleteList:[],
inputComplete:false,
inputCompleteStyle:{"width":"200px","border":"1px solid #ccc","background":"#FFFFFF"},

@input在输入框内容发生变化后触发

autoComplete(val){
        if(val.length == 0){
          this.inputComplete = false;
          this.filterInputCompleteList = [];
        }else{
          const data = {
            page:1,
            limit:10,
            chiShortName:val
          }
          area.getCountyList(data).then(response => {
            this.filterInputCompleteList = response.data
            if(this.filterInputCompleteList.length == 0){
              this.$message.error('未匹配到相关数据');
            }else{
              this.inputComplete = true;
              this.inputCompleteStyle = {"width":"240px","border":"1px solid #ccc","background":"#FFFFFF","margin-top":"-20px","margin-left":"120px","z-index":"999","position":"absolute"};
            }
          })
        }
      },

当用户点击列表中的项时,selectInputComplete 方法会被触发

//自动补全选择
      selectInputComlete(item){
        this.ruleForm.area = item.chiShortName;
        this.inputComplete = false;
        this.filterInputCompleteList = []; // 选择之后清空建议列表
      },

样式:

<style scoped>
  .complete_data{
    height: 30px;
    line-height: 30px;
    font-size: 12px;
    padding-left: 15px;
    cursor: pointer;
    border-bottom: 1px solid #ccc;
  }
</style>

效果:

 

标签:Vue,补全,item,列表,输入框,filterInputCompleteList,data
From: https://www.cnblogs.com/zwh0910/p/18123384

相关文章

  • Vscode设置自动生成vue页面代码块
    1.ctrl+shift+p 2. 输入snippet->首选项:配置用户代码片段 3. 输入vue,选择vue.json(vue) 4.输入下面代码:"Printtoconsole":{      "prefix":"vue",    "body":[      "<template>",      &qu......
  • vue项目的运行流程
    assets文件夹:存放项目中用到的静态资源文件,例如:css样式表、图片资源components文件夹:程序员封装的、可复用的组件,都要放到components目录下main.js是项目的入口文件。整个项目的运行,要先执行main.jsApp.vue是项目的根组件。在工程化的项目中,vue要做的事情很单纯:通过ma......
  • 基于SpringBoot+MySQL+SSM+Vue.js的生鲜在线销售系统(附论文)
    演示视频基于SpringBoot+MySQL+SSM+Vue.js的生鲜在线销售系统技术描述开发工具:Idea/Eclipse数据库:MySQLJar包仓库:Maven前端框架:Vue/ElementUI后端框架:Spring+SpringMVC+Mybatis+SpringBoot文字描述基于SpringBoot+MySQL+SSM+Vue.js的生鲜在线销售系统(附......
  • 用vue.js写案例——ToDoList待办事项 (步骤和全码解析)
     目录 一.准备工作二.编写各个组件的页面结构三.实现初始任务列表的渲染四.新增任务五.删除任务六.展示未完成条数七.切换状态-筛选数据八.待办事项(全)代码 一.准备工作在开发“ToDoList”案例之前,需要先完成一些准备工作,包括创建项目、引入BootStrap样式文......
  • 基于SpringBoot+MySQL+SSM+Vue.js的招聘系统(附论文)
    演示视频基于SpringBoot+MySQL+SSM+Vue.js的招聘系统技术描述开发工具:Idea/Eclipse数据库:MySQLJar包仓库:Maven前端框架:Vue/ElementUI后端框架:Spring+SpringMVC+Mybatis+SpringBoot文字描述基于SpringBoot+MySQL+SSM+Vue.js的招聘系统(附论文),用户,管理员......
  • 前端【Vuex】【使用介绍】
    1、组件下载vue与vuex的版本对应关系: Vue2匹配的Vuex3 Vue3匹配的Vuex4Vuex是一个专为Vue.js应用程序开发的状态管理模式+库。它采用集中式存储管理应用的所有组件的状态,并以相应的规则保证状态以一种可预测的方式发生变化。官方网......
  • 1.引入vue.js
    Vue2: https://v2.cn.vuejs.org/Vue3:https://cn.vuejs.org/在页面中通过script引入vue.js 方式一:下载vue.js  在文件中通过绝对路径引用 方式2:引用在线vue.js  ......
  • Vue实现手机APP页面的切换,如何使用Vue Router进行路由管理呢?
    在Vue中,实现手机APP页面的切换,通常会使用VueRouter进行路由管理。VueRouter是Vue.js官方的路由管理器,它和Vue.js深度集成,使构建单页面应用变得易如反掌。以下是一个简单的步骤说明,展示如何使用VueRouter实现手机APP页面的切换:安装VueRouter如果你还没有安装VueRouter,可......
  • vue-cli两种工程构建过程
    1、VueCLI工具入门1.1VueCLI的安装VueCLI是一个需要全局安装的NPM包,安装VueCLI的前提是设备已经安装了Node.js环境,如果你使用的是macOS操作系统,则系统默认会安装Node.js环境。如果系统没有安装,手动进行安装也非常简单。访问Node.js官网(https://nodejs.org),网页打开后,在页......
  • vue 全局组件 局部组件
    全局组件:<script>//创建vue实例constapp=Vue.createApp({template:`<div><hello/><world/><hello-world/></div>`});//子组件//组件具备复用性//全局组件,只要定义了,处处可以使用,性能不高,但是使用起来简单......