Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vue.js filterBy to search in multiple fields

Tags:

vue.js

How can I filter by searching in multiple search keys?

I'm trying something like this, but (of course) it won't work:

<tr v-repeat="questions | filterBy search in 'reference','user.name','shop.shopName'">
like image 558
user3711623 Avatar asked Jul 01 '15 09:07

user3711623


3 Answers

The filterBy custom filter is not documented AFAIK, but you can use a method to make your own filter:

var demo = new Vue({
    el: '#demo',
    data: {
        search: 're',

        people: [
          {name: 'Koos', age: 30, eyes:'red'},
          {name: 'Gert', age: 20, eyes:'blue'},
          {name: 'Pieter', age: 12, eyes:'green'},
          {name: 'Dawid', age: 67, eyes:'dark green'},
          {name: 'Johan', age: 15, eyes:'purple'},
          {name: 'Hans', age: 12, eyes:'pink'}
        ]
    },
    methods: {
      customFilter: function(person) {
          return person.name.indexOf(this.search) != -1
          || person.eyes.indexOf(this.search) != -1
          ;
      }
    },
   
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/1.0.26/vue.min.js"></script>
<div id="demo">
    <input type="text" class="form-control" v-model="search"/>
    
    <br/>
    <table class="table">
      <thead>
        <tr>
          <th>name</th>
          <th>eyes</th>
          <th>age</th>
        </tr>
      </thead>
      <tr v-for="person in people | filterBy customFilter">
        <td>{{ person.name }}</td>
        <td>{{ person.eyes }}</td>
        <td>{{ person.age }}</td>
      </tr>
  </table>
</div>
like image 121
Jannie Theunissen Avatar answered Oct 20 '22 04:10

Jannie Theunissen


This problem resolve with "Computed Properties":

var app = new Vue({
    el: '#demo',
    data: {
        search: '',
        people: [
          {name: 'Koos', age: 30, eyes:'red'},
          {name: 'Gert', age: 20, eyes:'blue'},
          {name: 'Pieter', age: 12, eyes:'green'},
          {name: 'Dawid', age: 67, eyes:'dark green'},
          {name: 'Johan', age: 15, eyes:'purple'},
          {name: 'Hans', age: 12, eyes:'pink'}
        ]
    },
    computed: {
        filteredPerson: function () {
            var self = this;
            return this.people.filter(function (person) {
                return person.name.toLowerCase().indexOf(self.search.toLowerCase()) >= 0
                || person.eyes.toLowerCase().indexOf(self.search.toLowerCase()) >= 0;
            });
        }
    }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.13/vue.min.js"></script>
<div id="demo">
    <input type="text" class="form-control" v-model="search"/>
    
    <br/>
    <table class="table">
      <thead>
        <tr>
          <th>name</th>
          <th>eyes</th>
          <th>age</th>
        </tr>
      </thead>
      <tr v-for="person in filteredPerson">
        <td>{{ person.name }}</td>
        <td>{{ person.eyes }}</td>
        <td>{{ person.age }}</td>
      </tr>
  </table>
</div>
like image 40
Mahdi Afzal Avatar answered Oct 20 '22 03:10

Mahdi Afzal


Since Vue.js version 0.12.11 this is possible with:

<li v-repeat="user in users | filterBy searchText in 'name' 'phone'"></li>

Make sure to checkout the official guide on this: http://vuejs.org/api/#orderBy

like image 45
kriskbx Avatar answered Oct 20 '22 04:10

kriskbx