国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

vue實現搜索功能

2024-05-06 15:38:52
字體:
來源:轉載
供稿:網友

本文實例為大家分享了vue實現搜索功能的具體代碼,供大家參考,具體內容如下

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

<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:[  //假數據  {name:"數據1",date:'2018-01-04',depart:'瀘化工1'},  {name:"數據2",date:'2018-01-25',depart:'瀘化工2'},  {name:"數據3",date:'2018-02-10',depart:'瀘化工3'},  {name:"數據4",date:'2018-03-04',depart:'瀘化工4'},  {name:"數據5",date:'2018-05-24',depart:'瀘化工5'},  {name:"數據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加載后馬上執行的,如賦值):

<!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()為數組中的每個元素執行一次callback函數,直到它找到一個返回值為可以轉化為布爾值true的值,此時some()方法將立刻返回true,否則立刻返回false 

by the way:

watch 它用于觀察Vue實例上的數據變動。對應一個對象,鍵是觀察表達式,值是對應回調。值也可以是方法名,或者是對象,包含選項。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 和林格尔县| 色达县| 临沭县| 南开区| 日土县| 乐都县| 新邵县| 通城县| 白朗县| 定州市| 永胜县| 乌拉特中旗| 景宁| 麟游县| 天气| 临汾市| 武邑县| 龙胜| 万载县| 陵川县| 宜良县| 汕尾市| 通许县| 东乌| 建阳市| 岳普湖县| 隆回县| 婺源县| 梅州市| 焉耆| 定州市| 浪卡子县| 邛崃市| 蕲春县| 讷河市| 江孜县| 兴海县| 手机| 安西县| 呼和浩特市| 新龙县|