基于element-ui中el-select下拉框選項過多的優(yōu)化方案
element-ui中el-select下拉框選項過多
el-select中options數(shù)據(jù)超過3000條就會造成前端頁面明顯卡頓,本次我的遇到的業(yè)務(wù)場景數(shù)據(jù)是近2w條,因此在不優(yōu)化的情況下頁面壓根無法使用,下面給出我的優(yōu)化過程。
一種優(yōu)化思路
element-ui的select有一個remote-method,支持遠(yuǎn)程搜索,我們讓服務(wù)端支持一下不就可以了,當(dāng)然這是一種解決的方案。
但是有時候這種方法對我并能夠不適用,因?yàn)檫@樣會出現(xiàn)回顯問題,回顯的時候是還需拿到所需option;
我的做法
element-ui的select有一個fildter-method方法,我們可以通過這個方法來進(jìn)行過濾下拉項
假設(shè)我們有個下拉框是用來選擇用戶的
<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ù)及過濾方法:
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()
? ? }
? );
},需要注意的是在回顯時要從總的option(allUserList)中拿到所需要會顯的值,并加入到顯示的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比對,沒有則加入
? ? ? ? ? ? ? ? ? ? if (this.userList.every(item => item.value !== target.value)) {
? ? ? ? ? ? ? ? ? ? ? ? this.userList.unshift(target)
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? },ok,問題解決。
element-ui中el-select下拉框數(shù)據(jù)過多,用pinyin-match實(shí)現(xiàn)拼音、首字母、漢字等模糊搜索
人狠話不多,上圖!

pinyin-match庫
也是項目需要,由于下拉框的數(shù)據(jù)過多,使用人員不好選擇,就做個拼音,大小寫字母以及漢字的模糊搜索,結(jié)果就找到來這個 pinyin-match庫,能夠使用拼音快速檢索目標(biāo)。
特好用的,這個庫的作者是個好心人啊,既然好東西也不能藏著,分享給大家!
在線演示:http://laosep.top/pinyin-match/
在項目中的使用步驟
第一步:安裝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="請選擇" 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個數(shù)據(jù)賦值brand
this.brand = result
}else{
//沒有搜索到數(shù)據(jù),就還展示所有的brand
this.brand = this.copyBrand
}
},
}
}
</script>
這樣就可以實(shí)現(xiàn)下拉框選擇器的拼音、字母以及漢字的模糊搜索啦!
總結(jié)
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue?實(shí)現(xiàn)動態(tài)設(shè)置元素的高度
這篇文章主要介紹了在vue中實(shí)現(xiàn)動態(tài)設(shè)置元素的高度,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-08-08
vue3中通過遍歷傳入組件名稱動態(tài)創(chuàng)建多個component 組件
這篇文章主要介紹了vue3中通過遍歷傳入組件名稱動態(tài)創(chuàng)建多個component 組件,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-03-03
vue3+element?plus實(shí)現(xiàn)側(cè)邊欄過程
這篇文章主要介紹了vue3+element?plus實(shí)現(xiàn)側(cè)邊欄過程,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-03-03
vue?cli2?和?cli3去掉eslint檢查器報錯的解決
這篇文章主要介紹了vue?cli2?和?cli3去掉eslint檢查器報錯的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教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)動態(tài)顯示表單項填寫進(jìn)度功能
這篇文章主要介紹了Vue實(shí)現(xiàn)動態(tài)顯示表單項填寫進(jìn)度功能,此功能可以幫助用戶了解表單填寫的進(jìn)度和當(dāng)前狀態(tài),提高用戶體驗(yàn),通常實(shí)現(xiàn)的方式是在表單中添加進(jìn)度條,根據(jù)用戶填寫狀態(tài)動態(tài)更新進(jìn)度條,感興趣的同學(xué)可以參考下文2023-05-05
vue結(jié)合Axios+v-for列表循環(huán)實(shí)現(xiàn)網(wǎng)易健康頁面(實(shí)例代碼)
這篇文章主要介紹了vue結(jié)合Axios+v-for列表循環(huán)實(shí)現(xiàn)網(wǎng)易健康頁面,在項目下安裝axios,通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-03-03

