Vue Element 分組+多選+可搜索Select選擇器實(shí)現(xiàn)示例
最終效果(http://47.98.205.88:3000/mult_group_selection)見下圖。實(shí)際就是將element三種官方select實(shí)例整合起來,同時(shí)實(shí)現(xiàn)分組+多選+可搜索,此外點(diǎn)擊一級(jí)分組名可以實(shí)現(xiàn)全選/全不選。供有相關(guān)需求的開發(fā)者參考

準(zhǔn)備工作:
除了vue腳手架、element等必要包之外。該項(xiàng)目還用到了linq.js(https://github.com/mihaifm/linq),該工具可以快速從數(shù)組中查找所需內(nèi)容。
npm install --save linq
編輯build/webpack.base.conf.js
module:{
......
//添加
new webpack.ProvidePlugin({
Enumerable: "linq"
})
}
數(shù)據(jù)源格式:
var selectList = [
{
name:"",//一級(jí)名稱
CName:"", //二級(jí)名稱
value:"" //值
},
{name:"",CName:"",value:""},
......
]
實(shí)現(xiàn):
script
data (){
return{
selectModel: [],
multipleSelectOption:[],
}
},
methods:{
//將源數(shù)據(jù)轉(zhuǎn)成element所需格式
transMultipleSelectOption(){
var level1List = Enumerable.from(this.allSelectList).distinct("o=>o.name").toArray();
var newArr = level1List.map(item=>{
let children = Enumerable.from(this.allSelectList).where((o)=>{return o.name==item.name;}).toArray();
var options = children.map(itemc=>{
return {"name": itemc.CName, "value":itemc.value};
});
return {"name": item.name, "options":options}
});
this.multipleSelectOption = newArr;
},
//重置options(select自動(dòng)補(bǔ)全相關(guān))
remoteMethod(queryString, lists) { //lists:原始數(shù)據(jù)
var mappedList = Enumerable.from(lists).where((o)=>{return o.CName.indexOf(queryString)!=-1
|| o.name.indexOf(queryString)!=-1;}).toArray(); //找出匹配搜索關(guān)鍵字的數(shù)據(jù)集
var level1List = Enumerable.from(mappedList).distinct("o=>o.name").toArray(); //從所匹配的數(shù)據(jù)集中找出所有一級(jí)菜單集合(含去重)
//重新拼成element所需的數(shù)據(jù)格式
var newArr = level1List.map(item=>{
let children = Enumerable.from(mappedList).where((o)=>{return o.name==item.name;}).toArray();
var options = children.map(itemc=>{
return {"name": itemc.CName, "value":itemc.value};
});
return {"name": item.name, "options":options}
});
this.multipleSelectOption = newArr;
},
//點(diǎn)擊一級(jí)菜單全選/全不選實(shí)現(xiàn)
checkAll(value){ //value: 點(diǎn)擊的一級(jí)name
var list = Enumerable.from(this.multipleSelectOption).where((o)=>{return o.name==value;}).toArray();
var level2ValueList = Enumerable.from(list[0].options).select("o=>o.value").toArray(); //所有該一級(jí)下二級(jí)的value集合
var num=0;
level2ValueList.forEach((value)=>{
this.selectModel2.forEach((model, index)=>{
if(model==value){
this.selectModel2.splice(index, 1); //如有匹配,先刪除
num++;
return true;
}
})
})
if(num < level2ValueList.length){ //需要全選
this.selectModel2 = this.selectModel2.concat(level2ValueList); //合并數(shù)組
}
}
},
mounted: function(){
this.transMultipleSelectOption();
},
template
<el-select v-model="selectModel" multiple filterable remote reserve-keyword
placeholder="請(qǐng)輸入關(guān)鍵詞" :remote-method="(queryString)=>{
remoteMethod(queryString, allSelectList);
}">
<el-option-group v-for="group in multipleSelectOption"
:key="group.name"
:label="group.name"
@click.native="checkAll(group.name)">
<el-option v-for="item in group.options"
:key="item.value"
:label="item.name"
:value="item.value">
</el-option>
</el-option-group>
</el-select>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue限制輸入框只能輸入8位整數(shù)和2位小數(shù)的代碼
這篇文章主要介紹了vue限制輸入框只能輸入8位整數(shù)和2位小數(shù),文中我們使用v-model加watch 實(shí)現(xiàn)這一個(gè)功能,代碼簡(jiǎn)單易懂,需要的朋友可以參考下2019-11-11
Vuejs仿網(wǎng)易云音樂實(shí)現(xiàn)聽歌及搜索功能
這篇文章主要介紹了Vuejs仿網(wǎng)易云音樂實(shí)現(xiàn)聽歌及搜索功能,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-03-03
VUE中使用TypeScript裝飾器實(shí)現(xiàn)表單驗(yàn)證的全過程
這篇文章主要給大家介紹了關(guān)于如何在VUE中使用TypeScript裝飾器實(shí)現(xiàn)表單驗(yàn)證的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-03-03
前端設(shè)置cookie之vue-cookies使用及說明
這篇文章主要介紹了前端設(shè)置cookie之vue-cookies使用及說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05
Vue3響應(yīng)式對(duì)象Reactive和Ref的用法解讀
這篇文章主要介紹了Vue3響應(yīng)式對(duì)象Reactive和Ref的用法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09

