基于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)選擇用戶的
<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)拼音、首字母、漢字等模糊搜索
人狠話不多,上圖!
pinyin-match庫(kù)
也是項(xiàng)目需要,由于下拉框的數(shù)據(jù)過(guò)多,使用人員不好選擇,就做個(gè)拼音,大小寫(xiě)字母以及漢字的模糊搜索,結(jié)果就找到來(lái)這個(gè) pinyin-match庫(kù),能夠使用拼音快速檢索目標(biāo)。
特好用的,這個(gè)庫(kù)的作者是個(gè)好心人啊,既然好東西也不能藏著,分享給大家!
在線演示: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)文章
解決npm安裝錯(cuò)誤:No matching version found for&
這篇文章主要介紹了解決npm安裝錯(cuò)誤:No matching version found for XXX@3.3.6問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07vue項(xiàng)目報(bào)錯(cuò):Missing?script:"serve"的解決辦法
這篇文章主要給大家介紹了關(guān)于vue項(xiàng)目報(bào)錯(cuò):Missing?script:"serve"的解決辦法,"missing script: serve"是一個(gè)錯(cuò)誤信息,意味著在執(zhí)行啟動(dòng)腳本時(shí),找不到名為"serve"的腳本,需要的朋友可以參考下2023-11-11vue之組件內(nèi)監(jiān)控$store中定義變量的變化詳解
今天小編就為大家分享一篇vue之組件內(nèi)監(jiān)控$store中定義變量的變化詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-11-11VUE + OPENLAYERS實(shí)現(xiàn)實(shí)時(shí)定位功能
本系列文章介紹一個(gè)簡(jiǎn)單的實(shí)時(shí)定位示例,基于VUE + OPENLAYERS實(shí)現(xiàn)實(shí)時(shí)定位功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2021-09-09Vue.js組件使用開(kāi)發(fā)實(shí)例教程
Vue.js的組件可以理解為預(yù)先定義好了行為的ViewModel類(lèi)。這篇文章主要介紹了Vue.js組件使用開(kāi)發(fā)實(shí)例教程的相關(guān)資料,需要的朋友可以參考下2016-11-11查找Vue中下標(biāo)的操作(some和findindex)
這篇文章主要介紹了查找Vue中下標(biāo)的操作(some和findindex),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-08