javascript - How to use filterBy in Vue.js -
i putting simple book sorter app , having trouble using filterby
filter. filter works fine filtering non-fiction
genre
reason not work fiction
. want display book titles , authors, , in input field, filter through list of books being displayed genre. if write fiction in input field, should display fiction books. view looks like:
<div id="app"> <h1>{{title}}</h1> <div id="input"> <input type="text" v-model="genre" placeholder="search fiction/non-fiction"> </div> <div id="display-books" v-for=" book in books | filterby genre in 'genre' "> <span>title: {{book.title}}</span> <span>author: {{book.author}}</span> </div> </div>
and here app.js
:
var appdata = { title: 'book sorter', filters: 'search filter', filtertext: '* show title if book available', genre: '', books: [ {genre: "fiction", title: "the hobbit", author: "j.r.r tolkien", available: false}, {genre: "non-fiction", title: "homage catalonia", author: "george orwell", available: true}, {genre: "non-fiction", title: "the tipping point", author: "malcolm gladwell", available: false}, {genre: "fiction", title: "lord of flies", author: "william golding", available: true}, {genre: "fiction", title: "the call of wild", author: "jack london", available: true} ] } new vue({ el: "#app", data: appdata, });
there no problem setup, vue's filterby working fine in example. when type fiction
, show books because have word fiction
in genre, meaning that: fiction
, non-fiction
both contains fiction
, returning true
.
create custom filter this:
vue.filter('match', function (value, input) { return input ? value.filter(function(item) { return item.genre === input ? value : null; }) : value; });
and change selection input dropdown:
<select v-model="genre"> <option value="" selected>select genre</option> <option value="fiction">fiction</option> <option value="non-fiction">non-fiction</option> </select>
here fiddle implementing it: https://jsfiddle.net/crabbly/br8huz7c/
Comments
Post a Comment