VUE側(cè)邊導(dǎo)航欄實(shí)現(xiàn)篩選過(guò)濾的示例代碼
需求
實(shí)現(xiàn)側(cè)邊導(dǎo)航欄的同時(shí),需要實(shí)現(xiàn)篩選功能。且由于導(dǎo)航名稱中可能包含大寫(xiě)英文字母,所以還需要支持小寫(xiě)英文字母篩選。
篩選效果:


清除篩選條件:

思路
- 監(jiān)聽(tīng)
el-input的回車(chē)事件:@keydown.enter.native - 將父組件傳過(guò)來(lái)的導(dǎo)航欄數(shù)據(jù)進(jìn)行深拷貝一層,并新建一個(gè)數(shù)組
newMenuList進(jìn)行存儲(chǔ)匹配的導(dǎo)航欄數(shù)據(jù):const copySideMenuList = JSON.parse(JSON.stringify(this.sideMenuList));- 深拷貝一層,為了防止原數(shù)據(jù)在搜索一次后被篡改,因?yàn)橐玫氖峭粋€(gè)內(nèi)存地址
- 將
el-input中輸入的字符串中的小寫(xiě)字母轉(zhuǎn)為大寫(xiě)字母:event = event.toUpperCase(); - 使用
includes方法判斷導(dǎo)航欄的名字中是否有el-input中的數(shù)據(jù),如果有則將這一項(xiàng)整個(gè)push到newMenuList中,最后判斷newMenuList的length是否大于0,如果大于0則將newMenuList賦值給導(dǎo)航欄組件需要的數(shù)據(jù)中,如果newMenuList的length不大于0則進(jìn)行提示:未匹配到該菜單 - 當(dāng)
el-input執(zhí)行clearable操作或者el-input中輸入為空時(shí),將父組件傳過(guò)來(lái)的導(dǎo)航欄數(shù)據(jù)賦值到導(dǎo)航欄組件所需要的數(shù)據(jù)中
實(shí)現(xiàn)
- 這里使用到
el-input的clearable,@clear,@keydown.enter.native - 注意:因?yàn)槭?code>el-input標(biāo)簽的本質(zhì)其實(shí)是
elementUI的一個(gè)組件,所以監(jiān)聽(tīng)回車(chē)鍵需要添加事件修飾符.native,它允許我們?cè)谧远x的組件標(biāo)簽中添加原生的DOM事件- 其他的
elementUI組件也是可以用這個(gè)方法來(lái)綁定原生DOM事件 - 而
el-button之所以可以直接使用@click是因?yàn)?code>elementUI在創(chuàng)建組件時(shí)就為<el-button>里面的<button>標(biāo)簽綁定了一個(gè)click事件,相當(dāng)于是官方提前為我們準(zhǔn)備好了一個(gè)click接口,我們看似是在調(diào)用原生js的click事件,實(shí)際上是在調(diào)用elementUI官方為我們提前準(zhǔn)備好的方法。
- 其他的

主要代碼如下:
<el-input placeholder="輸入關(guān)鍵字進(jìn)行過(guò)濾" v-model="filterText" class="side-menu__filter" size="small" clearable suffix-icon="el-icon-search" @clear="handleSearch(filterText)" @keydown.enter.native="handleSearch(filterText)"></el-input>
handleSearch(event) {
// 將將輸入的字符串中的小寫(xiě)字母轉(zhuǎn)為大寫(xiě)字母
event = event.toUpperCase();
let newMenuList = [];
// 將父組件傳過(guò)來(lái)的導(dǎo)航欄數(shù)據(jù)進(jìn)行深拷貝一層
const copySideMenuList = JSON.parse(JSON.stringify(this.sideMenuList));
copySideMenuList.forEach((firstItem) => {
// 循環(huán)判斷導(dǎo)航欄數(shù)據(jù)是否匹配到搜索的字符
if (firstItem.label.includes(event)) {
newMenuList.push(firstItem);
}
if (!firstItem.label.includes(event) && firstItem.children) {
for (let secondItem of firstItem.children) {
if (secondItem.label.includes(event)) {
newMenuList.push(secondItem);
}
}
}
});
if (newMenuList.length > 0) {
this.menuList = newMenuList;
} else {
this.$message.warning('未匹配到該菜單');
}
// 搜索字符串為空時(shí)返回原數(shù)據(jù)
if (event == '') {
this.menuList = this.sideMenuList;
}
}到此這篇關(guān)于VUE側(cè)邊導(dǎo)航欄實(shí)現(xiàn)篩選過(guò)濾的示例代碼的文章就介紹到這了,更多相關(guān)VUE側(cè)邊導(dǎo)航欄篩選過(guò)濾內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue中uni-app 實(shí)現(xiàn)小程序登錄注冊(cè)功能
這篇文章主要介紹了uni-app 實(shí)現(xiàn)小程序登錄注冊(cè)功能,文中給大家介紹了實(shí)現(xiàn)思路,以及vuex和本地緩存的區(qū)別,需要的朋友可以參考下2019-10-10
Vue Element前端應(yīng)用開(kāi)發(fā)之動(dòng)態(tài)菜單和路由的關(guān)聯(lián)處理
這篇文章主要介紹了Vue Element前端應(yīng)用開(kāi)發(fā)之動(dòng)態(tài)菜單和路由的關(guān)聯(lián)處理,對(duì)vue感興趣的同學(xué),可以參考下2021-05-05
vue/cli和vue版本對(duì)應(yīng)及安裝方式
這篇文章主要介紹了vue/cli和vue版本對(duì)應(yīng)及安裝方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10
解決Vue項(xiàng)目中Emitted value instead of an 
這篇文章主要介紹了解決Vue項(xiàng)目中Emitted value instead of an instance of Error問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11

