Vue實(shí)現(xiàn)簡(jiǎn)單搜索功能的示例代碼
1、概述
在vue項(xiàng)目中,搜索功能是我們經(jīng)常需要使用的一個(gè)場(chǎng)景,最常用的是在列表數(shù)據(jù)中搜索一個(gè)想要的,今天的例子就是我們實(shí)現(xiàn)vue從列表數(shù)據(jù)中搜索,并展示。如下圖所示:
2、功能邏輯
2.1功能流程
這里我們進(jìn)行簡(jiǎn)單搜索功能,搜索邏輯是只要用戶(hù)輸入值與產(chǎn)品的名稱(chēng)進(jìn)行模糊匹配,符合條件的數(shù)據(jù)進(jìn)行展示,不符合條件的數(shù)據(jù)過(guò)濾。流程描述如下:
- 用戶(hù)點(diǎn)擊搜索框,輸入內(nèi)容;
- 檢測(cè)到搜索框值變化,取用戶(hù)輸入值;
- 對(duì)用戶(hù)輸入值進(jìn)行判斷,若為空,則展示原列表,不為空則進(jìn)行篩選;
- 將用戶(hù)輸入值與所有列表數(shù)據(jù)進(jìn)行遍歷匹配,若匹配,則展示匹配條目,完成搜索。
2.2 流程圖
這張圖僅代表最簡(jiǎn)單的搜索流程,若需要進(jìn)行一些復(fù)雜的處理,可以進(jìn)行修改,比如說(shuō)匹配的商品名稱(chēng)關(guān)鍵字變色等。
3、功能實(shí)現(xiàn)
3.1 vue組件化
實(shí)現(xiàn)如上圖功能,我們肯定是使用vue的組件特性,將搜索以及,產(chǎn)品列表抽成組件,以提高代碼復(fù)用性,抽成組件之后,該頁(yè)面將由三個(gè)部分組成,數(shù)據(jù)在以下三個(gè)組件之間傳遞:
- 父組件:主頁(yè)面,用于數(shù)據(jù)邏輯處理;
- 搜索組件:給父組件傳遞用戶(hù)輸入值;
- 列表:展示從父組件接收的值。
3.2 代碼
父組件:index.vue
<template> <div> <title-bar :title="title" @goBack="goback"></title-bar> <search-input :plhText="searchPlhText" @input-changed="searchInputChange" ></search-input> <div v-for="(prd,index) in productListRst" :key="index"> <prd-item :prd="prd" @toPrdDetail="toPrdDetail"></prd-item> </div> </div> </template> <script> import TitleBar from "@/components/TitleBar"; import SearchInput from "./components/SearchInput"; import PrdItem from './components/PrdItem'; export default { name: "", components: { TitleBar, SearchInput, PrdItem }, data() { return { title: "產(chǎn)品列表", searchPlhText: "請(qǐng)輸入產(chǎn)品名稱(chēng)", productList: {}, // 產(chǎn)品列表 productListRst: {}, // 搜索篩選之后的產(chǎn)品列表 } }, created() { // 初始化頁(yè)面參數(shù),按照生命周期,子組件需要的參數(shù)父組件需要在created生命周期取值 this.initParams(); }, methods: { // 返回方法 goback() { // this.$emit("GoBack"); }, // 初始化頁(yè)面參數(shù),獲取產(chǎn)品列表 initParams() { this.productList = [ { imgPath: 'apple-1001.png', name: 'Apple iPad Air 平板電腦(2020新款)', price: '4799.00', sale: '5', ranking: '10000+評(píng)價(jià) 平板熱賣(mài)第5名', prdShopName: 'Apple官方旗艦店' }, { imgPath: 'apple-1002.png', name: 'Apple iPhone 11手機(jī)', price: '4999.00', sale: '5', ranking: '375萬(wàn)+評(píng)價(jià)', prdShopName: 'Apple官方旗艦店' }, { imgPath: 'apple-1003.jpg', name: 'Apple AirPods 配充電盒 Apple藍(lán)牙耳機(jī)', price: '1246.00', sale: '5', ranking: '200萬(wàn)+評(píng)價(jià)', prdShopName: 'Apple官方旗艦店' }, ]; this.productListRst = this.productList; }, // 每次search框變化則進(jìn)行篩選,對(duì)數(shù)據(jù)進(jìn)行邏輯處理 searchInputChange(value) { // 若未輸入值,則展示所有數(shù)據(jù) if(null === value || undefined === value){ this.productListRst = this.productList; } else { this.productListRst = []; // 結(jié)果列表置空 let regStr = ''; // 初始化正則表達(dá)式 for(let i=0; i<value.length; i++){ regStr = regStr + '(' + value[i] + ')([\\s]*)'; //跨字匹配 } let reg = new RegExp(regStr); console.log(reg); for(let i=0; i<this.productList.length; i++){ let name = this.productList[i].name; //按照名字匹配 let regMatch = name.match(reg); if(null !== regMatch) {// 將匹配的數(shù)據(jù)放入結(jié)果列表中 this.productListRst.push(this.productList[i]); } } } }, // 去往產(chǎn)品詳情頁(yè) toPrdDetail(){ this.$router.push({path: 'detail'}) } } }; </script> <style scoped> #page-title { width: 100%; background-color: #fff; display: flex; justify-content: center; } .backImg { width: 20px; } </style>
主要的邏輯處理是 searchInputChange,對(duì)于更復(fù)雜的搜索邏輯,也可以在里面進(jìn)行處理。
搜索組件:searchInput.vue
<template> <div class="search-box"> <div class="search-input"> <img src="@/assets/images/search.png" /> <input type="text" :placeholder="plhText" maxlength="10" @change="inputChange" v-model="inputValue" /> </div> </div> </template> <script> export default { name: "searchInput", // 搜索輸入框 props: { // input框占位文字 plhText: { type: String, default: "請(qǐng)輸入搜索內(nèi)容" } }, data() { return { inputValue: "" //輸入框的值 }; }, methods: { // 每次輸入框變化刷新列表 inputChange() { // 使用emit給父組件傳值 this.$emit('input-changed', this.inputValue); } } }; </script>
列表組件:productList.vue
<template> <div class="prd-item" @click="toPrdDetail"> <img :src="require('@/assets/images/'+prd.imgPath)"> <div class="prd-discription"> <div class="prd-title">{{ prd.name }}</div> <div class="prd-sellInfo"> <div class="prd-price">{{ prd.price }}</div> <div class="prd-saleLable" v-if="prd.sale.lenght!==0"> <label>12期免息</label> <span>新品</span> </div> <div class="prd-ranking">{{ prd.ranking }}</div> <div class="prd-shop">{{ prd.prdShopName }}</div> </div> </div> </div> </template> <script> export default { props: { // 傳入產(chǎn)品對(duì)象,必傳屬性為imgPath,name,price,shop prd: { type: Object, } }, methods: { // 跳轉(zhuǎn)產(chǎn)品詳情頁(yè)面 toPrdDetail() { this.$emit("to-prd-detail",this.prd.Id); } } }; </script>
標(biāo)題頭組件:titleBar
<template> <div class="page-title"> <div class="backImg" @click="goBack"> <img src="@/assets/images/arrow_left.png" /> </div> <div class="titleText"> <label>{{ title }}</label> </div> <div v-if="showRightArea" class="rightImg"> <img :src="rightImgPath" /> </div> </div> </template> <script> export default { name: "titleBar", props: { // 標(biāo)題 title: { type: String, //規(guī)定數(shù)據(jù)類(lèi)型 default: "標(biāo)題" //默認(rèn)值 }, showRightArea: { type: Boolean, default: false }, rightImgPath: { type: String } }, methods: { // 返回方法 goBack() { this.$emit("go-back"); } } }; </script> <style lang="scss" scoped> .page-title { width: 100%; background-color: #fff; display: flex; padding: 10px 0; justify-content: flex-start; } .backImg { width: 15%; text-align: center; img { margin: 0 auto; width: 24px; } } .titleText { font-size: 18px; height: 26px; width: 70%; text-align: center; label { margin: 0 auto; } } .rightImg { width: 15%; text-align: center; img { margin: 0 auto; width: 24px; } } </style>
以上代碼為部分代碼,css代碼不包含在內(nèi)。
3.3 動(dòng)態(tài)效果
以上代碼實(shí)現(xiàn)的效果如下動(dòng)態(tài)圖:
到此這篇關(guān)于Vue實(shí)現(xiàn)簡(jiǎn)單搜索功能的示例代碼的文章就介紹到這了,更多相關(guān)Vue搜索功能內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue中關(guān)于v-for循環(huán)key值問(wèn)題的研究
這篇文章主要介紹了vue中關(guān)于v-for循環(huán)key值問(wèn)題的研究,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06vue?首頁(yè)加載,速度優(yōu)化及解決首頁(yè)白屏的問(wèn)題
這篇文章主要介紹了vue?首頁(yè)加載,速度優(yōu)化及解決首頁(yè)白屏的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。2022-10-10vue移動(dòng)端模態(tài)框(可傳參)的實(shí)現(xiàn)
這篇文章主要介紹了vue移動(dòng)端模態(tài)框(可傳參)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11vue 子組件watch監(jiān)聽(tīng)不到prop的解決
這篇文章主要介紹了vue 子組件watch監(jiān)聽(tīng)不到prop的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-08Vue中使用Printjs插件實(shí)現(xiàn)打印功能
Print.js 主要是為了幫助我們直接在我們的應(yīng)用程序中打印 PDF 文件,無(wú)需離開(kāi)界面,也無(wú)需使用嵌入,這篇文章主要介紹了Vue中使用Printjs插件實(shí)現(xiàn)打印功能,需要的朋友可以參考下2022-08-08淺談vue項(xiàng)目,訪問(wèn)路徑#號(hào)的問(wèn)題
這篇文章主要介紹了淺談vue項(xiàng)目,訪問(wèn)路徑#號(hào)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-08vue+element-ui?校驗(yàn)開(kāi)始時(shí)間與結(jié)束時(shí)間的實(shí)現(xiàn)代碼
這篇文章主要介紹了vue+element-ui?校驗(yàn)開(kāi)始時(shí)間與結(jié)束時(shí)間的代碼實(shí)現(xiàn),最主要的需求是開(kāi)始時(shí)間不能早于當(dāng)前時(shí)間,感興趣的朋友跟隨小編一起看看吧2024-07-07在Vue3中使用BabylonJs開(kāi)發(fā)?3D的初體驗(yàn)
這篇文章主要介紹了在?Vue3?中使用?BabylonJs?開(kāi)發(fā)?3D?是什么體驗(yàn),在本文中,向您展示了如何創(chuàng)建?Vue?組件、Babylon?類(lèi)、在畫(huà)布上渲染場(chǎng)景以及創(chuàng)建?3D?網(wǎng)格,需要的朋友可以參考下2022-07-07