vue和js中實(shí)現(xiàn)模糊查詢方式
vue和js實(shí)現(xiàn)模糊查詢
先來(lái)看效果圖

這種數(shù)據(jù)量少的場(chǎng)景適用于前端實(shí)現(xiàn)模糊查詢
如何實(shí)現(xiàn)?
<template>
<div class="container">
<div class="search-bar">
<el-input v-model="inputVal" placeholder="請(qǐng)輸入圖標(biāo)名稱(chēng)" suffix-icon="el-icon-search" clearable></el-input>
</div>
<div class="icon-contain">
<div class="icon-item" v-for="item in searchList" :key="item.icon">
<i class="icon-style" :class="item.icon"></i>
<span class="icon-name">{{ item.name }}</span>
</div>
</div>
</div>
</template>
js部分
<script>
export default {
data () {
return {
inputVal: '',
list: [], // 全部圖標(biāo)列表
}
},
created() {
this.loadData()
},
computed: {
searchList () {
....
}
},
methods: {
loadData() { // 初始化數(shù)據(jù)
this.list = [
{ icon: 'el-icon-delete', name: 'el-icon-delete' },
{ icon: 'el-icon-setting', name: 'el-icon-setting' },
{ icon: 'el-icon-user', name: 'el-icon-user' },
{ icon: 'el-icon-star-off', name: 'el-icon-star-off' },
{ icon: 'el-icon-picture-outline', name: 'el-icon-picture-outline' },
{ icon: 'el-icon-s-custom', name: 'el-icon-s-custom' },
{ icon: 'el-icon-edit', name: 'el-icon-edit' },
{ icon: 'el-icon-folder-opened', name: 'el-icon-folder-opened' },
{ icon: 'el-icon-chat-dot-round', name: 'el-icon-chat-dot-round' },
{ icon: 'el-icon-upload', name: 'el-icon-upload' },
{ icon: 'el-icon-message-solid', name: 'el-icon-message-solid' }
]
},
}
}
</script>
我們用 computed 計(jì)算屬性來(lái)動(dòng)態(tài)獲得圖標(biāo)列表數(shù)據(jù)
想要達(dá)到的功能是:輸入框輸入內(nèi)容,列表框能動(dòng)態(tài)根據(jù)輸入值顯示輸入值相關(guān)的列表,即模糊查詢
方法1
使用 filter() 和 includes() 實(shí)現(xiàn)
computed: {
searchList () {
if (!this.inputVal) {
return this.list
}
return this.list.filter(item => {
return item.name.includes(this.inputVal)
})
return data
}
},
方法2
使用 indexOf() 實(shí)現(xiàn),(涉及到英文字母的還是建議處理一下大小寫(xiě)問(wèn)題)
computed: {
searchList () {
if (!this.inputVal) {
return this.list
}
const data = []
this.list.forEach(item => {
if (item.name.toLowerCase().indexOf(this.inputVal.toLowerCase()) !== -1) {
data.push(item)
}
})
return data
}
},
方法3
使用 test() 正則匹配實(shí)現(xiàn)
語(yǔ)法:RegExp.test(string) ,string 要檢測(cè)的字符串
該方法用于檢測(cè)一個(gè)字符串是否匹配某個(gè)模式,如果匹配返回 true ,否則返回 false
computed: {
searchList () {
if (!this.inputVal) {
return this.list
}
const data = []
const reg = new RegExp(this.inputVal.toLowerCase())
this.list.forEach(item => {
if (reg.test(item.name.toLowerCase())) {
data.push(item)
}
})
return data
}
},
還有其他的方法,如 split()、match() 等
正則表達(dá)式實(shí)現(xiàn)模糊查詢
拿到一個(gè)新需求 就是在前端進(jìn)行查詢一個(gè)數(shù)據(jù)
雖然一般這種行為都是在后端進(jìn)行但是有時(shí)候就是會(huì)給你這個(gè)需求,這里因?yàn)閿?shù)據(jù)量較小所以就進(jìn)行簡(jiǎn)單的遍歷查詢 沒(méi)有對(duì)數(shù)據(jù)進(jìn)行排序后查詢 其實(shí)有想過(guò)二分查找之類(lèi)的 但是這里數(shù)據(jù)量較小 就遍歷吧
? ? search() {
? ? ? console.log(this.searchInformation)
? ? ? this.listData = []
? ? ? var str = ".*" + this.searchInformation + ".*"
? ? ? let reg = new RegExp(str)
? ? ? //這里通過(guò)邀請(qǐng)碼查詢
? ? ? for(var i=0;i<this.wholeListData.length;i++){
? ? ? ? if(reg.test(this.wholeListData[i].content)){
? ? ? ? ? this.listData.push(this.wholeListData[i])
? ? ? ? }
? ? ? }
? ? ? //通過(guò)附加信息查詢
? ? ? for(var i=0;i<this.wholeListData.length;i++){
? ? ? ? if(reg.test(this.wholeListData[i].addition)){
? ? ? ? ? this.listData.push(this.wholeListData[i])
? ? ? ? }
? ? ? }
? ? },以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue中如何通過(guò)iframe方式加載本地的vue頁(yè)面
這篇文章主要介紹了vue中如何通過(guò)iframe方式加載本地的vue頁(yè)面,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09
項(xiàng)目中一鍵添加husky實(shí)現(xiàn)詳解
這篇文章主要為大家介紹了項(xiàng)目中一鍵添加husky實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
Element樹(shù)形控件el-tree實(shí)現(xiàn)一鍵全選、反選功能
最近做的項(xiàng)目用到了全選全不選功能,于是就自己動(dòng)手寫(xiě)了一個(gè),這篇文章主要給大家介紹了關(guān)于Element樹(shù)形控件el-tree實(shí)現(xiàn)一鍵全選、反選功能的相關(guān)資料,需要的朋友可以參考下2023-10-10
初識(shí)簡(jiǎn)單卻不失優(yōu)雅的Vue.js
這篇文章主要為大家介紹了簡(jiǎn)單卻不失優(yōu)雅、小巧而不乏大匠的Vue.js,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09
使用Vue綁定class和style樣式的幾種寫(xiě)法總結(jié)
這篇文章主要介紹了使用Vue綁定class和style樣式的幾種寫(xiě)法,文章通過(guò)代碼示例介紹的非常詳細(xì),具有一定的參考價(jià)值,需要的朋友可以參考下2023-07-07
vue(element ui)使用websocket及心跳檢測(cè)方式
這篇文章主要介紹了vue(element ui)使用websocket及心跳檢測(cè)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07
Vue2/3?登錄后用戶無(wú)操作半小時(shí)后自動(dòng)清除登錄信息退出登錄下線(示例代碼)
這篇文章主要介紹了Vue2/3?登錄后用戶無(wú)操作半小時(shí)后自動(dòng)清除登錄信息退出登錄下線,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2024-07-07

