1. 程式人生 > >VUE-搜索過濾器

VUE-搜索過濾器

div del log pan tolower product arch mode pos

首先引入Vue這個就不解釋了

HTML部分

    <div id="app">
        <input v-model=‘search‘ />
        <ul v-if="searchData.length > 0">
          <li v-for="item in searchData">{{item.name}},價格:¥{{item.price}}</li>
        </ul>
        <div v-else>暫無數據</div>
     </div
>
JS部分
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; } } })

非常實用把,試一下哦

VUE-搜索過濾器