本文介绍: element的el-autocomplete组件的使用(模糊匹配、自定义模板、远程搜索)
一、代码实现
<template>
<div>
<!-- trigger-on-focus 是否在输入框 focus 时显示建议列表 -->
<!-- autocomplete 是一个可带输入建议的输入框组件,fetch-suggestions 是一个返回输入建议的方法属性,如 querySearch(queryString, cb),
在该方法中你可以在你的输入建议数据准备好时通过 cb(data) 返回到 autocomplete 组件中。 -->
<!-- fetch-suggestions 返回输入建议的方法,仅当你的输入建议数据 resolve 时,通过调用 callback(data:[]) 来返回它 -->
<el-row class="demo-autocomplete">
<el-col :span="24">
<div class="sub-title">激活即列出输入建议</div>
<el-autocomplete class="inline-input" v-model="state1" :fetch-suggestions="querySearch" placeholder="请输入内容"
@select="handleSelect"></el-autocomplete>
</el-col>
<el-col :span="24">
<div class="sub-title">输入后匹配输入建议</div>
<el-autocomplete class="inline-input" v-model="state2" :fetch-suggestions="querySearch" placeholder="请输入内容"
:trigger-on-focus="false" @select="handleSelect"></el-autocomplete>
</el-col>
<el-col :span="24">
<div class="sub-title">自定义模板</div>
<el-autocomplete class="inline-input" v-model="state3" :fetch-suggestions="querySearch" placeholder="请输入内容"
:trigger-on-focus="true" @select="handleSelect">
<i class="el-icon-edit el-input__icon" slot="suffix" @click="handleIconClick">
</i>
<template slot-scope="{ item }">
<div class="name">{{ item.value }}</div>
<span class="addr" style="color:#b4b4b4;">{{ item.address }}</span>
</template>
</el-autocomplete>
</el-col>
<el-col :span="24">
<div class="sub-title">远程搜索</div>
<el-autocomplete v-model="state4" :fetch-suggestions="querySearchAsync" placeholder="请输入内容"
@select="handleSelect"></el-autocomplete>
</el-col>
</el-row>
</div>
</template>
<script>
export default {
name: 'AutoComplete',
data() {
return {
restaurants: [],
state1: '',
state2: '',
state3: '',
state4: '',
};
},
mounted() {
this.restaurants = this.loadAll();
},
methods: {
// 获取数据
loadAll() {
return [
{ "value": "三全鲜食(北新泾店)", "address": "长宁区新渔路144号" },
{ "value": "Hot honey 首尔炸鸡(仙霞路)", "address": "上海市长宁区淞虹路661号" },
{ "value": "新旺角茶餐厅", "address": "上海市普陀区真北路988号创邑金沙谷6号楼113" },
{ "value": "泷千家(天山西路店)", "address": "天山西路438号" },
{ "value": "胖仙女纸杯蛋糕(上海凌空店)", "address": "上海市长宁区金钟路968号1幢18号楼一层商铺18-101" },
{ "value": "贡茶", "address": "上海市长宁区金钟路633号" },
];
},
querySearch(queryString, callback) {
console.log(queryString, 'queryString')
var restaurants = this.restaurants;
var results = queryString ? restaurants.filter(this.createFilter(queryString)) : restaurants;
// 调用 callback 返回建议列表的数据
callback(results);
},
// 根据queryString进行筛选
createFilter(queryString) {
return (restaurant) => {
return (restaurant.value.toLowerCase().indexOf(queryString.toLowerCase()) === 0);
};
},
// 选中
handleSelect(item) {
console.log(item);
},
// 点击图标
handleIconClick(ev) {
console.log(ev, 'ev');
},
// ---------------------------------------------远程搜索相关------------------------------------
querySearchAsync(queryString, cb) {
var restaurants = this.restaurants;
var results = queryString ? restaurants.filter(this.createStateFilter(queryString)) : restaurants;
// 实际项目,从接口获取数据
clearTimeout(this.timeout);
this.timeout = setTimeout(() => {
cb(results);
}, 3000 * Math.random());
},
createStateFilter(queryString) {
return (state) => {
return (state.value.toLowerCase().indexOf(queryString.toLowerCase()) === 0);
};
},
},
}
</script>
<style scoped>
.sub-title{
margin-top: 10px;
margin-bottom: 10px;
}
</style>
二、效果图
原文地址:https://blog.csdn.net/weixin_41549971/article/details/135653189
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_59458.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。