If you are looking for real-time results, it's important to adjust your filter to return items that partially match.
One way to achieve this is by utilizing the startsWith()
method. The below filter utilizes startsWith()
to identify all items that begin with the specified input:
Vue.filter('byName', function (data, input) {
if(input){
return data.filter(function (item) {
// check if the item's name starts with the input
return item.name.startsWith(input);
});
}
return data;
});
Experience this filter in action through the following JSFiddle link.
Alternatively, if you prefer to retrieve items that match anywhere within their content, rather than just those starting with the input, you can opt for String.prototype.indexOf()
.
To implement this change, simply substitute
// we check if the item's name starts with the input
return item.name.startsWith(input);
with
// we check if the item's name contains the input as a substring
return item.name.indexOf(input) > -1;