vue實(shí)現(xiàn)搜索功能
本文實(shí)例為大家分享了vue實(shí)現(xiàn)搜索功能的具體代碼,供大家參考,具體內(nèi)容如下
methods (要有一定的觸發(fā)條件才能執(zhí)行,如點(diǎn)擊事件)
<template>
<div class="safetyInfo">
<input type="text" name="" id="" placeholder="搜索" v-model="search"/>
<button @click="btn">搜索</button>
<ul v-for="list in searchData">
<li>
<span>{{list.name}}</span>
<span>{{list.date}}</span>
<span>{{list.depart}}</span>
</li>
</ul>
</div>
</template>
<script>
export default {
name: 'HelloWorld',
data () {
return {
search:'',
searchData: '',
products:[
//假數(shù)據(jù)
{name:"數(shù)據(jù)1",date:'2018-01-04',depart:'瀘化工1'},
{name:"數(shù)據(jù)2",date:'2018-01-25',depart:'瀘化工2'},
{name:"數(shù)據(jù)3",date:'2018-02-10',depart:'瀘化工3'},
{name:"數(shù)據(jù)4",date:'2018-03-04',depart:'瀘化工4'},
{name:"數(shù)據(jù)5",date:'2018-05-24',depart:'瀘化工5'},
{name:"數(shù)據(jù)6",date:'2018-10-29',depart:'瀘化工6'}
]
}
},
methods:{
btn:function(){
var search = this.search;
if (search) {
this.searchData = this.products.filter(function(product) {
console.log(product)
return Object.keys(product).some(function(key) {
console.log(key)
return String(product[key]).toLowerCase().indexOf(search) > -1
})
})
}
}
}
}
</script>
computed (在HTML DOM加載后馬上執(zhí)行的,如賦值):
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script src="https://unpkg.com/vue/dist/vue.js"></script>
</head>
<body>
<div id="app">
<input v-model='search' />
<ul v-for="item in searchData ">
<li>{{item.name}},價(jià)格:¥{{item.price}}</li>
</ul>
</div>
<script>
var vm = new Vue({
el: '#app',
data: {
search: '',
products: [{
name: '蘋(píng)果',
price: 25,
category: "水果"
}, {
name: '香蕉',
price: 15,
category: "水果"
}, {
name: '雪梨',
price: 65,
category: "水果"
}, {
name: '寶馬',
price: 2500,
category: "汽車(chē)"
}, {
name: '奔馳',
price: 10025,
category: "汽車(chē)"
}, {
name: '柑橘',
price: 15,
category: "水果"
}, {
name: '奧迪',
price: 25,
category: "汽車(chē)"
}]
},
computed: {
searchData: function() {
var search = this.search;
if (search) {
return this.products.filter(function(product) {
return Object.keys(product).some(function(key) {
return String(product[key]).toLowerCase().indexOf(search) > -1
})
})
}
return this.products;
}
}
})
</script>
</body>
</html>
注:some()為數(shù)組中的每個(gè)元素執(zhí)行一次callback函數(shù),直到它找到一個(gè)返回值為可以轉(zhuǎn)化為布爾值true的值,此時(shí)some()方法將立刻返回true,否則立刻返回false
by the way:
watch 它用于觀(guān)察Vue實(shí)例上的數(shù)據(jù)變動(dòng)。對(duì)應(yīng)一個(gè)對(duì)象,鍵是觀(guān)察表達(dá)式,值是對(duì)應(yīng)回調(diào)。值也可以是方法名,或者是對(duì)象,包含選項(xiàng)。
所以他們的執(zhí)行順序?yàn)椋耗J(rèn)加載的時(shí)候先computed再watch,不執(zhí)行methods;等觸發(fā)某一事件后,則是:先methods再watch。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 基于Vue.js實(shí)現(xiàn)簡(jiǎn)單搜索框
- 利用vue + element實(shí)現(xiàn)表格分頁(yè)和前端搜索的方法
- Vue.js實(shí)現(xiàn)多條件篩選、搜索、排序及分頁(yè)的表格功能
- Vue Element 分組+多選+可搜索Select選擇器實(shí)現(xiàn)示例
- Vue el-autocomplete遠(yuǎn)程搜索下拉框并實(shí)現(xiàn)自動(dòng)填充功能(推薦)
- vue組件實(shí)踐之可搜索下拉框功能
- 基于vue實(shí)現(xiàn)可搜索下拉框定制組件
- vuejs通過(guò)filterBy、orderBy實(shí)現(xiàn)搜索篩選、降序排序數(shù)據(jù)
- vue2.0多條件搜索組件使用詳解
- vue實(shí)現(xiàn)實(shí)時(shí)搜索顯示功能
相關(guān)文章
關(guān)于vue 的slot分發(fā)內(nèi)容 (多個(gè)分發(fā))
這篇文章主要介紹了關(guān)于vue 的slot分發(fā)內(nèi)容 (多個(gè)分發(fā)),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
vue3.0使用mapState,mapGetters和mapActions的方式
這篇文章主要介紹了vue3.0使用mapState,mapGetters和mapActions的方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06
vue npm install 安裝某個(gè)指定的版本操作
這篇文章主要介紹了vue npm install 安裝某個(gè)指定的版本操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-08
VUE中如何調(diào)用高德地圖獲取當(dāng)前位置(VUE2.0和3.0通用)
使用uniapp開(kāi)發(fā)微信小程序時(shí),多多少少會(huì)遇到獲取當(dāng)前位置的詳細(xì)信息,下面這篇文章主要給大家介紹了關(guān)于VUE中如何調(diào)用高德地圖獲取當(dāng)前位置(VUE2.0和3.0通用)的相關(guān)資料,需要的朋友可以參考下2023-04-04
vue使用lottie-web實(shí)現(xiàn)web動(dòng)畫(huà)效果
在web端,lottie-web庫(kù)可以解析導(dǎo)出的動(dòng)畫(huà)json文件,并將其以svg或者canvas的方式將動(dòng)畫(huà)繪制在我們的頁(yè)面上,這篇文章主要介紹了vue使用lottie-web實(shí)現(xiàn)web動(dòng)畫(huà),需要的朋友可以參考下2024-06-06
vue+element-plus上傳圖片及回顯問(wèn)題及數(shù)量限制
本文主要介紹了vue+element-plus上傳圖片及回顯問(wèn)題及數(shù)量限制,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04

