vue實(shí)現(xiàn)下拉多選、可搜索、全選功能(示例代碼)
最后的效果就是樹形的下拉多選,可選擇任意一級選項(xiàng),下拉框中有一個(gè)按鈕可以實(shí)現(xiàn)全選,也支持搜索功能。

在mounted生命周期里面獲取全部部門的數(shù)據(jù),handleTree是講接口返回的數(shù)據(jù)整理成樹形結(jié)構(gòu),可以自行解決
<div class="LeftText">
<span style="color: red; margin-right: 4px">*</span>部門:
</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:[];//所有的部門信息,內(nèi)部結(jié)構(gòu)為:{deptId:1,deptName:"一級部門"}
isSeach:false;//是否搜索狀態(tài)
tempExecutive:[];// 搜索前已選中的數(shù)據(jù)
//搜索查詢事件--是因?yàn)樵赾ascaderChange事件中,對v-model的值重新賦值,導(dǎo)致下拉選時(shí),會觸發(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();
}
},
對全選狀態(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框里回顯的是選中部門的名稱
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)行合并和去重,否則,之前選中的值會被清空
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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue實(shí)現(xiàn)靜態(tài)頁面點(diǎn)贊和取消點(diǎn)贊功能
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)靜態(tài)頁面點(diǎn)贊和取消點(diǎn)贊的功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02
Vue實(shí)現(xiàn)自定義視頻和圖片上傳的示例代碼
這篇文章主要為大家詳細(xì)介紹了如何通過Vue實(shí)現(xiàn)自定義視頻和圖片上傳的功能,文中的示例代碼簡潔易懂,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-04-04
jenkins自動(dòng)構(gòu)建發(fā)布vue項(xiàng)目的方法步驟
這篇文章主要介紹了jenkins自動(dòng)構(gòu)建發(fā)布vue項(xiàng)目的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
vue3中使用viewerjs實(shí)現(xiàn)圖片預(yù)覽效果的項(xiàng)目實(shí)踐
viewer.js是一款開源的圖片預(yù)覽插件,功能十分強(qiáng)大,本文主要介紹了vue3中使用viewerjs實(shí)現(xiàn)圖片預(yù)覽效果的項(xiàng)目實(shí)踐,具有一定的參考價(jià)值,感興趣的可以了解一下2023-09-09
VUE對Storage的過期時(shí)間設(shè)置,及增刪改查方式
這篇文章主要介紹了VUE對Storage的過期時(shí)間設(shè)置,及增刪改查方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-02-02
vue實(shí)現(xiàn)拖拽或點(diǎn)擊上傳圖片
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)拖拽或點(diǎn)擊上傳圖片,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09
vue插件開發(fā)之使用pdf.js實(shí)現(xiàn)手機(jī)端在線預(yù)覽pdf文檔的方法
這篇文章主要介紹了vue插件開發(fā)之使用pdf.js實(shí)現(xiàn)手機(jī)端在線預(yù)覽pdf文檔的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-07-07
在Vue3項(xiàng)目中使用VueCropper裁剪組件實(shí)現(xiàn)裁剪及預(yù)覽效果
這篇文章主要介紹了在Vue3項(xiàng)目中使用VueCropper裁剪組件(裁剪及預(yù)覽效果),本文分步驟結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-07-07

