這篇文章主要介紹使用vue實(shí)現(xiàn)搜索功能的示例,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
中寧網(wǎng)站制作公司哪家好,找創(chuàng)新互聯(lián)!從網(wǎng)頁設(shè)計(jì)、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、成都響應(yīng)式網(wǎng)站建設(shè)公司等網(wǎng)站項(xiàng)目制作,到程序開發(fā),運(yùn)營維護(hù)。創(chuàng)新互聯(lián)從2013年成立到現(xiàn)在10年的時(shí)間,我們擁有了豐富的建站經(jīng)驗(yàn)和運(yùn)維經(jīng)驗(yàn),來保證我們的工作的順利進(jìn)行。專注于網(wǎng)站建設(shè)就選創(chuàng)新互聯(lián)。
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: '蘋果', 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ù)組中的每個(gè)元素執(zhí)行一次callback函數(shù),直到它找到一個(gè)返回值為可以轉(zhuǎn)化為布爾值true的值,此時(shí)some()方法將立刻返回true,否則立刻返回false
by the way:
watch 它用于觀察Vue實(shí)例上的數(shù)據(jù)變動(dòng)。對(duì)應(yīng)一個(gè)對(duì)象,鍵是觀察表達(dá)式,值是對(duì)應(yīng)回調(diào)。值也可以是方法名,或者是對(duì)象,包含選項(xiàng)。
所以他們的執(zhí)行順序?yàn)椋耗J(rèn)加載的時(shí)候先computed再watch,不執(zhí)行methods;等觸發(fā)某一事件后,則是:先methods再watch。
以上是“使用vue實(shí)現(xiàn)搜索功能的示例”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
當(dāng)前標(biāo)題:使用vue實(shí)現(xiàn)搜索功能的示例
網(wǎng)頁網(wǎng)址:http://muchs.cn/article6/jiooog.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站制作、網(wǎng)站導(dǎo)航、企業(yè)網(wǎng)站制作、網(wǎng)站收錄、網(wǎng)站維護(hù)、云服務(wù)器
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)