According to the information provided in the Documentation, it is mentioned that the filter filterBy
is specifically designed for directives that require Array
values, such as v-for
.
As for the arguments:
- For targetStringOrFunction argument :
If you have an array of strings and wish to filter it based on a value from an input field, you can achieve this by utilizing filterBy
in the following manner:
new Vue({
el: '...',
data: {
searchedValue: '' // Leaving it empty will display all results
}
...
});
In the HTML:
<div v-for="value in values | filterBy searchedValue">
You can connect the searchedValue
to an input field for added functionality.
Refer to the example below for using functions.
- For
in
and {String} [...searchKeys]
arguments :
If you have an array of users, with properties like firstName
and lastName
, and want to filter them based on a specific value, you can employ filterBy
as shown below:
<div v-for="user in users | filterBy searchedValue in 'firstName'">
Alternatively, you can search through multiple keys:
<div v-for="user in users | filterBy searchedValue in 'firstName' 'lastName'">
Additionally, you can create a function to encapsulate your code like this:
var demo = new Vue({
el: '#demo',
data: {
searchedValue: '',
users: [
{firstName: 'John', lastName:'Doe'},
{firstName: 'David', lastName:'Bazz'},
{firstName: 'Peter', lastName:'Foo'},
]
},
methods: {
myFilter: function(user) {
return user.firstName == searchedValue; //Or any other condition you prefer
}
},
})
I hope that helps :)