vue Element-ui input 遠(yuǎn)程搜索與修改建議顯示模版的示例代碼
html:
<template> <el-autocomplete popper-class="my-autocomplete" custom-item="my-remote" v-model="state" :fetch-suggestions="querySearch" placeholder="默認(rèn)空" icon="close" :on-icon-click="handleIconClick"> </el-autocomplete> </template>
js:
<script>
import Vue from 'vue'
Vue.component('my-remote', {
functional: true,
render: function(h, ctx) {
var item = ctx.props.item;
let str = h('li', ctx.data, [
h('div', { attrs: { class: 'name' } }, [item.value]),
h('span', { attrs: { class: 'addr' } }, [item.address])
]);
if (item.str) { // 根據(jù)參數(shù)不同 修改原模版結(jié)構(gòu)
str = h('center', { attrs: { class: 'ems' } }, [item.str])
}
return str
},
props: {
item: { type: Object, required: true }
}
});
export default {
data() {
return {
restaurants: [],
state: '',
timeout: null,
_that: {} // 記錄this,用來發(fā)起http請求
};
},
methods: {
querySearch(queryString, cb) {
let restaurants = this.restaurants;
if (restaurants.length > 0) { // 如果參數(shù)都沒變化,則使用緩存數(shù)據(jù),避免請求沉積
let results = queryString ? restaurants.filter(this.createFilter(queryString)) : restaurants;
cb(results);
} else {
const qtype = ‘參數(shù)';
this._that.$http('/inner', { qtype: qtype })
.then((res) => {
restaurants = this.loadAll(res);
this.restaurants = restaurants;
let results = queryString ? restaurants.filter(this.createFilter(queryString)) : restaurants;
cb(results);
})
.catch((err) => {
restaurants = this.loadAll();
let results = queryString ? restaurants.filter(this.createFilter(queryString)) : restaurants;
cb(results);
});
}
},
createFilter(queryString) {
return (restaurant) => {
if (restaurant.str) return false;
return (restaurant.value.indexOf(queryString.toLowerCase()) === 0);
};
},
loadAll(data) {
let serier = [];
if (data) {
for (let i = 0, l = data.length; i < l; i++) {
let a = data[i];
let b = '';
if (typeof a === "object") {
b = a[1];
a = a[0];
}
serier.push({ "value": a, "address": b })
}
} else { // 如果沒有請求到數(shù)據(jù),則顯示暫無數(shù)據(jù)!
serier.push({ "str": '暫無數(shù)據(jù)' })
}
return serier;
},
handleIconClick(ev) {
this.state = "";
}
},
mounted() {
this._that = this;
}
}
</script>
css:
<style lang="scss">
.my-autocomplete {
li {
line-height: normal !important;
padding: 7px !important;
.name {
text-overflow: ellipsis;
overflow: hidden;
}
.addr {
font-size: 12px;
color: #b4b4b4;
}
.highlighted .addr {
color: #ddd;
}
}
.ems {
font-size: 12px;
color: #b4b4b4;
}
}
</style>
總結(jié)
以上所述是小編給大家介紹的vue Element-ui input 遠(yuǎn)程搜索與修改建議顯示模版的示例代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
elementUI el-form 數(shù)據(jù)無法賦值且不報(bào)錯(cuò)解決方法
本文主要介紹了elementUI el-form 數(shù)據(jù)無法賦值且不報(bào)錯(cuò)解決方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-12-12
vue.js 中使用(...)運(yùn)算符報(bào)錯(cuò)的解決方法
這篇文章主要介紹了vue.js 中使用(...)運(yùn)算符報(bào)錯(cuò)的解決方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-08-08
vuex獲取state對象中值的所有方法小結(jié)(module中的state)
這篇文章主要介紹了vuex獲取state對象中值的所有方法小結(jié)(module中的state),具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04
vue2結(jié)合element-ui實(shí)現(xiàn)TreeSelect樹選擇功能
這篇文章主要為大家詳細(xì)介紹了vue2如何結(jié)合element-ui實(shí)現(xiàn)TreeSelect樹選擇功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解下2024-03-03
Vue3中的極致防抖/節(jié)流詳解(附常見方式防抖/節(jié)流)
在JavaScript中函數(shù)的防抖和節(jié)流不是什么新鮮話題,這篇文章主要給大家介紹了關(guān)于Vue3中極致防抖/節(jié)流的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-02-02
vue3+vite+antd如何實(shí)現(xiàn)自定義主題
這篇文章主要介紹了vue3+vite+antd如何實(shí)現(xiàn)自定義主題問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03
vue頁面離開后執(zhí)行函數(shù)的實(shí)例
下面小編就為大家分享一篇vue頁面離開后執(zhí)行函數(shù)的實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-03-03
vue3使用Element-plus的el-pagination分頁組件時(shí)無法顯示中文
本文主要介紹了vue3使用Element-plus的el-pagination分頁組件時(shí)無法顯示中文,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-12-12

