vue實(shí)現(xiàn)下拉多選、可搜索、全選功能(示例代碼)
最后的效果就是樹(shù)形的下拉多選,可選擇任意一級(jí)選項(xiàng),下拉框中有一個(gè)按鈕可以實(shí)現(xiàn)全選,也支持搜索功能。
在mounted生命周期里面獲取全部部門(mén)的數(shù)據(jù),handleTree是講接口返回的數(shù)據(jù)整理成樹(shù)形結(jié)構(gòu),可以自行解決
<div class="LeftText"> <span style="color: red; margin-right: 4px">*</span>部門(mén): </div> <el-select v-model="executiveDepartName" filterable :filter-method="selectChange" multiple @visible-change="visibleChange" @remove-tag="seleRemoveTag" style="width: 80%" > <el-option style="display: none" value=""></el-option> <el-checkbox style=" width: 100%; height: 40px; line-height: 40px; padding-left: 20px; border-bottom: 1px solid #dcdfe6; " class="allselect" :indeterminate="isIndeterminate" v-model="allSelectModule" @change="allselect" >全選</el-checkbox > <el-cascader-panel ref="cascaderModule" :key="deptList.length" :options="deptList" @change="cascaderChange" style="width: 80%" :props="props" filterable :border="false" :show-all-levels="false" v-model="executiveDepartment" > </el-cascader-panel> </el-select> </div>
props: { multiple: true, value: "deptId", label: "deptName", checkStrictly: true, emitPath: false, }, allDeptList:[];//所有的部門(mén)信息,內(nèi)部結(jié)構(gòu)為:{deptId:1,deptName:"一級(jí)部門(mén)"} isSeach:false;//是否搜索狀態(tài) tempExecutive:[];// 搜索前已選中的數(shù)據(jù) //搜索查詢(xún)事件--是因?yàn)樵赾ascaderChange事件中,對(duì)v-model的值重新賦值,導(dǎo)致下拉選時(shí),會(huì)觸發(fā)el-select的搜索事件,所以加了一個(gè)isFilter判斷 selectChange(val) { if (val !== "") { this.deptList = []; this.deptList = this.allDeptList.filter((item) => { return item.deptName.toLowerCase().indexOf(val.toLowerCase()) > -1; }); this.isSeach = true; this.tempExecutive = this.executiveDepartment; } else { if (!this.isFilter) { this.deptList = this.handleTree(this.allDeptList, "deptId"); this.isFilter = !this.isFilter; } } }, visibleChange(e) { if (e) { this.isSeach = false; this.isFilter = false; this.deptList = this.handleTree(this.allDeptList, "deptId"); this.initStatus(); } }, 對(duì)全選狀態(tài)進(jìn)行重新賦值 initStatus() { if (this.executiveDepartment.length == this.allDeptList.length) { this.allSelectModule = true; this.isIndeterminate = false; } else if (this.executiveDepartment.length == 0) { this.allSelectModule = false; this.isIndeterminate = false; } else { this.allSelectModule = false; this.isIndeterminate = true; } }, //select框里回顯的是選中部門(mén)的名稱(chēng) getDeptName() { const result = []; this.executiveDepartment.filter((item) => { this.allDeptList.map((i) => { if (item == i.deptId) { result.push(i.deptName); } }); }); return result; }, seleRemoveTag(val) { if (val) { const result = this.allDeptList.find((item) => { if (item.deptName == val) { return item; } }); this.executiveDepartment = this.executiveDepartment.filter( (item) => item !== result.deptId ); } }, // 下拉多選選中時(shí)觸發(fā)的事件 cascaderChange() { this.isFilter = true; //如果是搜索狀態(tài),講之前選中的值和搜素狀態(tài)下的值進(jìn)行合并和去重,否則,之前選中的值會(huì)被清空 if (this.isSeach) { this.executiveDepartment = [ ...new Set([...this.tempExecutive, ...this.executiveDepartment]), ]; } this.executiveDepartName = this.getDeptName(); this.initStatus(); }, //全選事件 allselect() { if (this.allSelectModule) { this.isIndeterminate = false; if (this.isSeach) { this.executiveDepartment = this.deptList.map((item) => item.deptId); this.executiveDepartName = this.getDeptName(); } else { this.executiveDepartment = this.getAllIds(this.deptList); this.executiveDepartName = this.getDeptName(); } } else { this.executiveDepartment = []; this.executiveDepartName = []; } }, getAllIds(nodes) { let ids = []; (function getIds(nodes) { nodes.forEach((node) => { ids.push(node.deptId); if (node.children && node.children.length) { getIds(node.children); } }); })(nodes); return ids; },
到此這篇關(guān)于vue實(shí)現(xiàn)下拉多選、可搜索、全選功能的文章就介紹到這了,更多相關(guān)vue下拉框多選內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue-cli如何關(guān)閉Uncaught?error的全屏提示
這篇文章主要介紹了vue-cli如何關(guān)閉Uncaught?error的全屏提示問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-04-04一文詳解vue各種權(quán)限控制與管理實(shí)現(xiàn)思路
這篇文章主要為大家介紹了vue各種權(quán)限控制與管理的實(shí)現(xiàn)思路詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03vue3+ts+vite+electron搭建桌面應(yīng)用的過(guò)程
這篇文章主要介紹了vue3+ts+vite+electron搭建桌面應(yīng)用的過(guò)程,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-04-04vue + typescript + 極驗(yàn)登錄驗(yàn)證的實(shí)現(xiàn)方法
這篇文章主要介紹了vue + typescript + 極驗(yàn) 登錄驗(yàn)證的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06vue之父子組件間通信實(shí)例講解(props、$ref、$emit)
組件間如何通信,也就成為了vue中重點(diǎn)知識(shí)了。這篇文章將會(huì)通過(guò)props、$ref和 $emit 這幾個(gè)知識(shí)點(diǎn),來(lái)講解如何實(shí)現(xiàn)父子組件間通信。2018-05-05vue實(shí)現(xiàn)網(wǎng)易云音樂(lè)純界面
這篇文章主要為大家介紹了vue實(shí)現(xiàn)網(wǎng)易云音樂(lè)純界面過(guò)程詳解,附含詳細(xì)源碼,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07vue的style綁定background-image的方式和其他變量數(shù)據(jù)的區(qū)別詳解
今天小編就為大家分享一篇vue的style綁定background-image的方式和其他變量數(shù)據(jù)的區(qū)別詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-09-09