Filter list with Vue.js

前端 未结 4 1775
我在风中等你
我在风中等你 2020-11-30 04:07

I just got started with Vue.js and here is what I\'m doing: I am rendering a list of products, and each product has a name, a gender and a si

相关标签:
4条回答
  • 2020-11-30 04:22
    computed: {
            filteredItems() {
              return this.allStartupData.filter(item => {
                let byName =
                  item.name.toLowerCase().indexOf(this.search.toLowerCase()) > -1;
                let byDescription =
                  item.description.toLowerCase().indexOf(this.search.toLowerCase()) >
                  -1;
                if (byName === true) {
                  return byName;
                } else if (byDescription === true) {
                  return byDescription;
                }
              });
            }
          }
    

    and then u can iterate through filteredItems like e.g

     <v-flex v-for="(obj,index) in filteredItems" :key="index" xs12 md4>
    
    0 讨论(0)
  • 2020-11-30 04:24

    You can try v-if="!gender || product.gender == gender"

    0 讨论(0)
  • 2020-11-30 04:29

    Just modified @Nora's answer.

    You need to change in the template as:

     <div id="product_index">
        <div v-for="product in products" v-if="!gender || product.gender===gender">
          <p>{{product.name}}<p>
        </div>
        <input v-on:change="updateGender">
      </div>
    

    and in JS file as:

    var vm = new Vue({
          el: '#product_index',
          data: {
            gender: "",
            products: [{name: "jean1", gender: "women", size: "S"}, {name: "jean2", gender: "men", size: "S"}]
          },
          methods:{
            updateGender: function(event){
              this.gender = event.target.value
            }
          }
        }
      );
    

    Working Demo: https://jsbin.com/qocuraquki/edit?html,js,console,output

    0 讨论(0)
  • 2020-11-30 04:31

    Use computed properties - something like this (Example bellow filter items by type)

    const app = new Vue({
    
      el: '#app',
    
      data: {
         search: '',
         items: [
           {name: 'Stackoverflow', type: 'development'},
           {name: 'Game of Thrones', type: 'serie'},
           {name: 'Jon Snow', type: 'actor'}
         ]
      },
    
      computed: {
        filteredItems() {
          return this.items.filter(item => {
             return item.type.toLowerCase().indexOf(this.search.toLowerCase()) > -1
          })
        }
      }
    
    })
    

    Template:

      <div id="app">
    
        <div v-for="item in filteredItems" >
          <p>{{item.name}}</p>
        </div>
    
        <input type="text" v-model="search">
    
      </div>
    

    Demo: http://jsbin.com/dezokiwowu/edit?html,js,console,output

    0 讨论(0)
提交回复
热议问题