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)擊一級分組名可以實(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:"",//一級名稱 CName:"", //二級名稱 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自動補(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ù)集中找出所有一級菜單集合(含去重) //重新拼成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)擊一級菜單全選/全不選實(shí)現(xiàn) checkAll(value){ //value: 點(diǎn)擊的一級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(); //所有該一級下二級的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="請輸入關(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>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue 使用html2canvas將DOM轉(zhuǎn)化為圖片的方法
這篇文章主要介紹了vue 使用html2canvas將DOM轉(zhuǎn)化為圖片的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-09-09vue element-ui表格自定義動態(tài)列具體實(shí)現(xiàn)
這周工作中遇見了一個(gè)表格動態(tài)列的需求,下面這篇文章主要給大家介紹了關(guān)于vue element-ui表格自定義動態(tài)列具體實(shí)現(xiàn)的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-06-06vue中$set的使用(結(jié)合在實(shí)際應(yīng)用中遇到的坑)
這篇文章主要介紹了vue中$set的使用(結(jié)合在實(shí)際應(yīng)用中遇到的坑),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-07-07解讀element?el-upload上傳的附件名稱不顯示?file-list賦值
這篇文章主要介紹了解讀element?el-upload上傳的附件名稱不顯示?file-list賦值問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-10-10