基于element-ui中el-select下拉框選項(xiàng)過(guò)多的優(yōu)化方案
element-ui中el-select下拉框選項(xiàng)過(guò)多
el-select中options數(shù)據(jù)超過(guò)3000條就會(huì)造成前端頁(yè)面明顯卡頓,本次我的遇到的業(yè)務(wù)場(chǎng)景數(shù)據(jù)是近2w條,因此在不優(yōu)化的情況下頁(yè)面壓根無(wú)法使用,下面給出我的優(yōu)化過(guò)程。
一種優(yōu)化思路
element-ui的select有一個(gè)remote-method,支持遠(yuǎn)程搜索,我們讓服務(wù)端支持一下不就可以了,當(dāng)然這是一種解決的方案。
但是有時(shí)候這種方法對(duì)我并能夠不適用,因?yàn)檫@樣會(huì)出現(xiàn)回顯問(wèn)題,回顯的時(shí)候是還需拿到所需option;
我的做法
element-ui的select有一個(gè)fildter-method方法,我們可以通過(guò)這個(gè)方法來(lái)進(jìn)行過(guò)濾下拉項(xiàng)
假設(shè)我們有個(gè)下拉框是用來(lái)選擇用戶(hù)的
<el-select ? v-model="userId" ? filterable ? :filter-method="userFilter" ? clearable> ? <el-option ? ? v-for="item in userList" ? ? :key="item.userId" ? ? :label="item.username" ? ? :value="item.userId" ? ></el-option> </el-select>
在這里將userId封裝成v-model:
props: {
? ? ? ? value: {
? ? ? ? ? ? type: [String, Number],
? ? ? ? ? ? default: ''
? ? ? ? }
? ? },
computed: {
? ? ? ? userId: {
? ? ? ? ? ? get () {
? ? ? ? ? ? ? ? return this.value || ''
? ? ? ? ? ? },
? ? ? ? ? ? set (value) {
? ? ? ? ? ? ? ? this.$emit('input', value)
? ? ? ? ? ? }
? ? ? ? }
? ? },獲取option數(shù)據(jù)及過(guò)濾方法:
userFilter(query = '') {
? let arr = this.allUserList.filter((item) => {
? ? return item.username.includes(query) || item.userId.includes(query)
? })
? if (arr.length > 50) {
? ? this.userList = arr.slice(0, 50)
? } else {
? ? this.userList = arr
? }
},
getUserWhiteList() {
? HttpRequest.post("/api/admin/community/getUserWhiteList").then(
? ? response => {
? ? ? this.allUserList = response.data.list;
? ? ? this.userFilter()
? ? }
? );
},需要注意的是在回顯時(shí)要從總的option(allUserList)中拿到所需要會(huì)顯的值,并加入到顯示的option(userList)中:
addValueOptions () {
? ? ? ? ? ? if (this.userId) {
? ? ? ? ? ? ? ? let target = this.allUserList.find((item) => { // 從大option中找到當(dāng)前條
? ? ? ? ? ? ? ? ? ? return item.value === this.userId
? ? ? ? ? ? ? ? })
? ? ? ? ? ? ? ? if (target) { // 將當(dāng)前條與小option比對(duì),沒(méi)有則加入
? ? ? ? ? ? ? ? ? ? if (this.userList.every(item => item.value !== target.value)) {
? ? ? ? ? ? ? ? ? ? ? ? this.userList.unshift(target)
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? },ok,問(wèn)題解決。
element-ui中el-select下拉框數(shù)據(jù)過(guò)多,用pinyin-match實(shí)現(xiàn)拼音、首字母、漢字等模糊搜索
人狠話(huà)不多,上圖!

pinyin-match庫(kù)
也是項(xiàng)目需要,由于下拉框的數(shù)據(jù)過(guò)多,使用人員不好選擇,就做個(gè)拼音,大小寫(xiě)字母以及漢字的模糊搜索,結(jié)果就找到來(lái)這個(gè) pinyin-match庫(kù),能夠使用拼音快速檢索目標(biāo)。
特好用的,這個(gè)庫(kù)的作者是個(gè)好心人啊,既然好東西也不能藏著,分享給大家!
在線(xiàn)演示:http://laosep.top/pinyin-match/
在項(xiàng)目中的使用步驟
第一步:安裝pinyin-match
// 安裝 pinyin-match npm install pinyin-match --save
第二步:在需要的組件中使用
利用el-select的filterable 屬性和filter-method方法使用模糊搜索
<template>
<el-select v-model="formInline.brandId" @change="changeBrand" filterable :filter-method="pinyingMatch" placeholder="請(qǐng)選擇" style="width: 180px" >
<el-option :label="item.label" :value="item.value" v-for="(item,index ) in brand" :key="item.value"></el-option>
</el-select>
</template>
<script>
export default{
data(){
return{
brand:[],//下拉框所有數(shù)據(jù)
copyBrand:[]
}
},
methods:{
//獲取所有的品牌
async getBrand(){
const response = await reqLimitBranch()
this.brand = response.data
//把所有的品牌在賦值copyBrand
this.copyBrand = this.brand
},
//下拉框設(shè)置拼音模糊搜索
pinyingMatch(val){
const pinyingMatch = require('pinyin-match')
if(val){
var result = []
//
this.copyBrand.forEach( e => {
var m = pinyingMatch.match(e.label, val)
if( m){
result.push(e)
}
})
//搜索到相應(yīng)的數(shù)據(jù)就把符合條件的n個(gè)數(shù)據(jù)賦值brand
this.brand = result
}else{
//沒(méi)有搜索到數(shù)據(jù),就還展示所有的brand
this.brand = this.copyBrand
}
},
}
}
</script>
這樣就可以實(shí)現(xiàn)下拉框選擇器的拼音、字母以及漢字的模糊搜索啦!
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue?實(shí)現(xiàn)動(dòng)態(tài)設(shè)置元素的高度
這篇文章主要介紹了在vue中實(shí)現(xiàn)動(dòng)態(tài)設(shè)置元素的高度,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08
vue3中通過(guò)遍歷傳入組件名稱(chēng)動(dòng)態(tài)創(chuàng)建多個(gè)component 組件
這篇文章主要介紹了vue3中通過(guò)遍歷傳入組件名稱(chēng)動(dòng)態(tài)創(chuàng)建多個(gè)component 組件,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-03-03
vue3+element?plus實(shí)現(xiàn)側(cè)邊欄過(guò)程
這篇文章主要介紹了vue3+element?plus實(shí)現(xiàn)側(cè)邊欄過(guò)程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03
vue?cli2?和?cli3去掉eslint檢查器報(bào)錯(cuò)的解決
這篇文章主要介紹了vue?cli2?和?cli3去掉eslint檢查器報(bào)錯(cuò)的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04
Vue.js實(shí)現(xiàn)的表格增加刪除demo示例
這篇文章主要介紹了Vue.js實(shí)現(xiàn)的表格增加刪除demo,結(jié)合實(shí)例形式分析了vue.js數(shù)據(jù)綁定及元素增加、刪除等相關(guān)操作技巧,需要的朋友可以參考下2018-05-05
Vue實(shí)現(xiàn)動(dòng)態(tài)顯示表單項(xiàng)填寫(xiě)進(jìn)度功能
這篇文章主要介紹了Vue實(shí)現(xiàn)動(dòng)態(tài)顯示表單項(xiàng)填寫(xiě)進(jìn)度功能,此功能可以幫助用戶(hù)了解表單填寫(xiě)的進(jìn)度和當(dāng)前狀態(tài),提高用戶(hù)體驗(yàn),通常實(shí)現(xiàn)的方式是在表單中添加進(jìn)度條,根據(jù)用戶(hù)填寫(xiě)狀態(tài)動(dòng)態(tài)更新進(jìn)度條,感興趣的同學(xué)可以參考下文2023-05-05
vue結(jié)合Axios+v-for列表循環(huán)實(shí)現(xiàn)網(wǎng)易健康頁(yè)面(實(shí)例代碼)
這篇文章主要介紹了vue結(jié)合Axios+v-for列表循環(huán)實(shí)現(xiàn)網(wǎng)易健康頁(yè)面,在項(xiàng)目下安裝axios,通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-03-03

