In Vue, I have to filter through some data:
<input v-model="search">
<ul>
<li v-repeat="photo in photos | filterBy search in 'name'">
<img src="{{ photo.src }}" alt="{{ photo.name }}">
</li>
<li v-if="!photos.length">
No results, sorry!
</li>
</ul>
How can I detect empty filter results and display an appropriate message to the user?
EDIT
I am currently doing the following which I feel is a hacky workaround:
HTML:
<input v-model="search">
<ul>
<li v-repeat="photo in photos">
<img src="{{ photo.src }}" alt="{{ photo.name }}">
</li>
<li v-if="!photos.length">
No results, sorry!
</li>
</ul>
Javascript:
var v = new Vue({
data: {
allPhotos: [...],
photos: [],
search: '',
},
ready: function () {
var filter = Vue.filter('filterBy');
var self = this;
this.$watch('search', function () {
self.photos = filter(self.allPhotos, self.search, 'name');
}, {
immediate: true
});
}
})
In Vue 2.x filters can now only be used, as docs say, inside text interpolations:
Vue 2.x filters can only be used inside mustache bindings. To achieve the same behavior inside directive bindings, you should use Computed properties instead.
You can achieve same behavior with JavaScript built-in filter
method and computed property.
<input v-model="searchQuery">
<span v-if="!filteredItems.length">No results.</span>
<ul>
<li v-for="item in filteredItems"></li>
</ul>
computed: {
filteredItems: function () {
var self = this;
return self.items.filter(function (item) {
return item.indexOf(self.searchQuery) !== -1;
})
}
}
There are two ways at the moment. In all cases, template can look the same.
<input v-model="searchQuery">
<span v-if="!filteredItems.length">No results.</span>
<ul>
<li v-for="item in filteredItems"></li>
</ul>
Original filterBy
method accessed via $options
.
computed: {
filteredItems: function () {
return this.$options.filters.filterBy(this.items, this.searchQuery);
}
}
A little bit cleaner approach. Eval expression like you would do inside your template.
computed: {
filteredItems: function () {
return this.$eval('items | filterBy searchQuery');
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With