当前位置:网站首页>el-autocomplete使用
el-autocomplete使用
2022-08-05 05:24:00 【MoXinXueWEB】
案例
案例显示效果:
代码实现:
**特别说明:**以下代码仅代表大概思路
<template>
<div>
<h1>搜索查寻</h1>
<el-autocomplete
v-model="inputValue"
:fetch-suggestions="querySearch"
:trigger-on-focus="false"
placeholder="请输入内容"
@select="handleSubmit"
@keyup.enter.native="handleSubmit"
@input="changeStyle('block', '.el-autocomplete-suggestion')"
@keyup="changeStyle('block', '.el-autocomplete-suggestion')"
></el-autocomplete>
</div>
</template>
<script>
export default {
data() {
return {
inputValue: "",
allInfos: [
{ "value": "abcddtrr", "name": "看自身情况" },
{ "value": "abc", "name": "看自身情况" },
{ "value": "dtrr", "name": "看自身情况" },
{ "value": "ddtrrdf", "name": "看自身情况" },
{ "value": "ccdvvffrf", "name": "看自身情况" },
{ "value": "adsadsdsd", "name": "看自身情况" },
{ "value": "vfrtssg", "name": "看自身情况" },
]
}
},
mounted() {},
methods: {
handleSubmit() {
// 在handleSubmit的时候调用changeStyle方法,传入的状态为none(表示让输入建议框隐藏)
this.changeStyle("none", ".el-autocomplete-suggestion");
//根据自身情况
//该方法是提交时触发
},
//输入框获取焦点时调用的方法
querySearch(queryString, cb) {
//allInfos是怎么来,是从父组件传过来的还是在自己组件调用api接口拿到的还是其他
//我这里的allInfos是从父组件传过来的,在这里也举例子组件从父组件传值
let results = this.allInfos;
results = queryString
? results.filter(this.createFilter(queryString))
: results;
// 控制展示条数,防止数据太大加载时间过长,影响体验
if(results.length > 100) {
results.splice(100,results. length-1);
}
// 返回筛选出的结果数据到输入框下面的输入列表
cb(results);
},
// 当输入数据时触发的,筛选出和输入数据匹配的建议输入。
// 调用match方法,是模糊匹配;官方调用的是indexOf,是精确匹配,看自身情况选择
createFilter(queryString) {
return (item) => {
return item.value.toUpperCase().match(queryString.toUpperCase());
};
},
//根据传进来的状态改变建议输入框的状态(展开|隐藏)
changeStyle(status, className) {
let dom = document.querySelectorAll(className);
dom[0].style.display = status;
},
}
}
方法详解
element UI的带输入建议官方文档
建议先看官方文档,这里是官方文档的适当补充
引用el-autocomplete
在需要的地方引用
<el-autocomplete
class="inline-input"
v-model="inputValue"
:fetch-suggestions="querySearch"
placeholder="请输入内容"
@select="handleSubmit"
></el-autocomplete>
v-model=“inputValue” :与inputValue绑定值,即,自动输入建议的值可以通过inputValue拿到。
:fetch-suggestions=“querySearch” :输入框一获取焦点。就会自动调用该方法拿到数据,这些数据就是模糊查询出来的数据。
@select=“handleSelect” : 当选中建议项时,调用该方法。
触发带输入建议的两种方式
- 1.输入框获取焦点时就触发
这是默认的
- 2.输入值后匹配触发
在组件上加上:trigger-on-focus=“false”
<el-autocomplete
class="inline-input"
v-model="inputValue"
:fetch-suggestions="querySearch"
:trigger-on-focus="false"
placeholder="请输入内容"
@select="handleSubmit"
></el-autocomplete>
fetch-suggestions方法的数据结构
文档里面,回调的数据结果是一个数组对象,对象里一定必须要有一个**value
的属性**。否则,待输入建议的数据是无法渲染出来。
[
{ "value": "xxx(在输入建议看到的值,必需)", "address": "看自身情况" },
]
如果,我们拿到的数据是这样的,虽然也是数组,但是数组里的对象属性不一样。
this.modelInfos=
[
{
"modelId": "1", "modelName": "a1",type:"c" },
{
"modelId": "2", "modelName": "a2",type:"c" },
{
"modelId": "3", "modelName": "a3",type:"c" },
{
"modelId": "4", "modelName": "a4",type:"c" },
]
可以使用map返回想要的数据结构。
this.allInfos= this.modelInfos.map((terminal) => {
return {
value: modelName,
name: modelId,
};
});
console.log(this.allInfo);
可能会遇到的问题
1、如何增加回车触发事件
在组件里增加 @keyup.enter.native方法
2、如何解决输入内容没有建议内容情况下,不显示下拉框
在组件里增加 trigger-on-focus属性
<el-autocomplete
class="inline-input"
v-model="inputValue"
:fetch-suggestions="querySearch"
:trigger-on-focus="false"
placeholder="请输入内容"
@select="handleSubmit"
@keyup.enter.native="handleSubmit"
></el-autocomplete>
3、如何解决回车后建议输入框没消失的bug
如果增加了回车事件,那么输入数据回车后,输入建议框没有自动消失,该如何解决?
在组件增加方法:@input(在输入值发生改变的时候触发changeStyle方法)
@keyup(按键松开触发的事件,也就是回车时触发changeStyle方法)
传入的“block”是让输入框建议展开,'.el-autocomplete-suggestion’是输入建议框的类名
<el-autocomplete
class="inline-input"
v-model="inputValue"
:fetch-suggestions="querySearch"
:trigger-on-focus="false"
placeholder="请输入内容"
@select="handleSubmit"
@keyup.enter.native="handleSubmit"
@input="changeStyle('block', '.el-autocomplete-suggestion')"
@keyup="changeStyle('block', '.el-autocomplete-suggestion')"
></el-autocomplete>
//根据传进来的状态改变建议输入框的状态(展开|隐藏)
changeStyle(status, className) {
let dom = document.querySelectorAll(className);
dom[0].style.display = status;
},
在handleSubmit的时候调用changeStyle方法,传入的状态为none(表示让输入建议框隐藏)
handleSubmit() {
this.changeStyle("none", ".el-autocomplete-suggestion");
},
边栏推荐
- Mina的长连接和短连接
- 入门文档01 series按顺序执行
- spark source code-RPC communication mechanism
- Regular expression small example - get number character and repeated the most
- Spark source code - task submission process - 4-container to start executor
- IP packet format (ICMP protocol and ARP protocol)
- VRRP概述及实验
- lvm逻辑卷及磁盘配额
- spark source code - task submission process - 3-ApplicationMaster
- Getting Started 11 Automatically add version numbers
猜你喜欢
【Machine Learning】1 Univariate Linear Regression
I/O性能与可靠性
Network wiring and digital-to-system conversion
LinkSLA insists that users come first and creates a sustainable operation and maintenance service plan
IP地址及子网的划分
Getting Started Document 07 Staged Output
I/O performance and reliability
Cloud computing - osi seven layers and TCP\IP protocol
【Day8】Knowledge about disk and disk partition
网络层协议介绍
随机推荐
TCP/IP four-layer model
Getting Started Documentation 12 webserve + Hot Updates
idea 常用快捷键
Getting Started Doc 08 Conditional Plugins
监控系统的内卷,有什么讲究?
Getting Started 05 Using cb() to indicate that the current task is complete
User and user group management, file permission management
Proprietary host CDH
spark source code-RPC communication mechanism
Growth: IT Operations Trends Report
[Day1] (Super detailed steps) Build a soft RAID disk array
sql server 重复值在后面计数
7 steps to complete cloud monitoring
618, you may be able to wake up a little bit
Advantages of overseas servers
The problem of redirecting to the home page when visiting a new page in dsf5.0
What's the point of monitoring the involution of the system?
正则表达式小示例--获取重复最多的字符及其数量
Introductory document 05-2 use return instructions the current task has been completed
运维的高光时刻,从智能化开始