I wanted to build a custom-filter for my v-data-table (containing books) which
I figured that using "value" in the customFilter function would require all of the search terms to appear in the same prop, so I used item instead, but vuetify gives the complete item to the filter function, not only the filterable headers of the data table, which will yield unwanted search results, so I'm re-initializing the item to get rid of unwanted props.
methods: {
customFilter(value, search, item) {
item = {
number: item.number,
title: item.title,
author: item.author
};
const haystack = Object.values(item).join().toLowerCase();
let s = search.toString().toLowerCase();
let needles = s.replace('-',' ').split(' ');
return needles.filter(needle => haystack.indexOf(needle) >= 0).length == needles.length
}
}
My questions
Answering my own questions:
My solution now:
methods: {
itemFilterMultipleSearchTermsAnd(value, search, item) {
const nonSearchableColumns = ["excludedPropName1", "excludedPropName2"];
const reducedObject = Object.keys(item)
.filter((key) => !nonSearchableColumns.includes(key))
.reduce((res, key) => ((res[key] = item[key]), res), {});
const haystack = Object.values(reducedObject)
.join()
.toLowerCase();
let s = search.toString().toLowerCase();
let needles = s.replace("-", " ").split(" ");
return (
needles.filter((needle) => haystack.indexOf(needle) >= 0).length ==
needles.length
);
}
}