溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

使用vue實現(xiàn)搜索功能的示例

發(fā)布時間:2021-04-02 11:08:17 來源:億速云 閱讀:339 作者:小新 欄目:web開發(fā)

這篇文章主要介紹使用vue實現(xiàn)搜索功能的示例,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!

methods (要有一定的觸發(fā)條件才能執(zhí)行,如點擊事件)

<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}},價格:¥{{item.price}}</li>
</ul>
</div>
 
 
<script>
var vm = new Vue({
el: '#app',
data: {
search: '',
products: [{
name: '蘋果',
price: 25,
category: "水果"
}, {
name: '香蕉',
price: 15,
category: "水果"
}, {
name: '雪梨',
price: 65,
category: "水果"
}, {
name: '寶馬',
price: 2500,
category: "汽車"
}, {
name: '奔馳',
price: 10025,
category: "汽車"
}, {
name: '柑橘',
price: 15,
category: "水果"
}, {
name: '奧迪',
price: 25,
category: "汽車"
}]
},
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ù)組中的每個元素執(zhí)行一次callback函數(shù),直到它找到一個返回值為可以轉(zhuǎn)化為布爾值true的值,此時some()方法將立刻返回true,否則立刻返回false 

by the way:

watch 它用于觀察Vue實例上的數(shù)據(jù)變動。對應(yīng)一個對象,鍵是觀察表達式,值是對應(yīng)回調(diào)。值也可以是方法名,或者是對象,包含選項。

所以他們的執(zhí)行順序為:默認加載的時候先computed再watch,不執(zhí)行methods;等觸發(fā)某一事件后,則是:先methods再watch。

以上是“使用vue實現(xiàn)搜索功能的示例”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

vue
AI