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)單搜索功能,搜索邏輯是只要用戶輸入值與產(chǎn)品的名稱(chēng)進(jìn)行模糊匹配,符合條件的數(shù)據(jù)進(jìn)行展示,不符合條件的數(shù)據(jù)過(guò)濾。流程描述如下:
- 用戶點(diǎn)擊搜索框,輸入內(nèi)容;
- 檢測(cè)到搜索框值變化,取用戶輸入值;
- 對(duì)用戶輸入值進(jìn)行判斷,若為空,則展示原列表,不為空則進(jìn)行篩選;
- 將用戶輸入值與所有列表數(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ù)邏輯處理;
- 搜索組件:給父組件傳遞用戶輸入值;
- 列表:展示從父組件接收的值。
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中scrollIntoView()方法詳解與實(shí)際運(yùn)用舉例
這篇文章主要給大家介紹了關(guān)于Vue中scrollIntoView()方法詳解與實(shí)際運(yùn)用舉例的相關(guān)資料,該scrollIntoView()方法將調(diào)用它的元素滾動(dòng)到瀏覽器窗口的可見(jiàn)區(qū)域,需要的朋友可以參考下2023-12-12
Vue+Node實(shí)現(xiàn)大文件上傳和斷點(diǎn)續(xù)傳
文件上傳在很多項(xiàng)目中都用的到,如果是幾M的很快就傳送完畢,如果是大文件呢?本文將利用Vue+Node實(shí)現(xiàn)大文件上傳和斷點(diǎn)續(xù)傳,感興趣的可以了解一下2022-04-04
Vue transition實(shí)現(xiàn)點(diǎn)贊動(dòng)畫(huà)效果的示例
點(diǎn)贊動(dòng)畫(huà)是網(wǎng)頁(yè)評(píng)論中常見(jiàn)的功能,本文將介紹如何用vue實(shí)現(xiàn)這一效果。點(diǎn)贊時(shí)愛(ài)心縮小變大,變大時(shí)略微大一點(diǎn)再變正常,取消點(diǎn)贊時(shí)愛(ài)心無(wú)動(dòng)畫(huà),同時(shí)數(shù)字滾動(dòng),+1 時(shí)向上滾動(dòng),-1 時(shí)向下滾動(dòng)2021-05-05
vue如何動(dòng)態(tài)實(shí)時(shí)的顯示時(shí)間淺析
這篇文章主要給大家介紹了關(guān)于vue如何動(dòng)態(tài)實(shí)時(shí)的顯示時(shí)間,以及vue時(shí)間戳 獲取本地時(shí)間實(shí)時(shí)更新的相關(guān)資料,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05
vue中計(jì)算屬性computed和普通屬性method的區(qū)別小結(jié)
Vue.js中Computed和Methods是兩種常用的數(shù)據(jù)處理方式,本文主要介紹了vue中計(jì)算屬性computed和普通屬性method的區(qū)別小結(jié),具有一定的參考價(jià)值,感興趣的可以了解一下2024-06-06

