Vue.js FilterBy

by Rodrigo

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/1.0.24/vue.min.js"></script>
<div id="app">
            <h1>{{title}}</h1>
              <div id="input">
               <select v-model="genre">
                 <option value="" selected>Select Genre</option> 
                 <option value="fiction">Fiction</option>
                 <option value="non-fiction">Non-Fiction</option>
               </select>
            </div>
             <div id="display-books" v-for=" book in books | match genre in 'genre' ">
               <span>Title: {{book.title}}</span>
               <span>Author: {{book.author}}</span>      
        </div>  
    </div>

JavaScript

var appData = { 
  title: 'Book Sorter',
  filters: 'Search filter',
  filtertext: '* only show title if book is available',
  genre: '',
  books:
    [
      {genre: "fiction", title: "The Hobbit", author: "J.R.R Tolkien", available: false},
      {genre: "non-fiction", title: "Homage to Catalonia", author: "George Orwell", available: true},
      {genre: "non-fiction", title: "The Tipping Point", author: "Malcolm Gladwell", available: false},
      {genre: "fiction", title: "Lord of the Flies", author: "William Golding", available: true},
      {genre: "fiction", title: "The Call of the Wild", author: "Jack London", available: true}   
    ]

}

Vue.filter('match', function (value, input) {
  return input ? value.filter(function(item) {
     return item.genre === input ? value : null;
  }) : value;
});

new Vue({
   el: "#app",
   data: appData,

});