Open sd4-github opened 1 year ago
If anyone is looking for a solution to not use type any
in https://github.com/solodynamo/ng2-search-filter/issues/55#issuecomment-607651243, try using template types instead of any
- in this I've removed the excludes
arg, however you can add it back as templated if you'd like:
// Created from https://github.com/solodynamo/ng2-search-filter/issues/55#issuecomment-607651243
import { Pipe, PipeTransform, Injectable } from '@angular/core';
@Pipe({
name: 'filter',
pure: false
})
@Injectable()
export class FilterPipe implements PipeTransform {
transform<T>(items: Array<T>, term: string): Array<T> {
if (!term || !items) return items;
return FilterPipe.filter(items, term);
}
static filter<T>(items: Array<T>, term: string): Array<T> {
const toCompare = term.toLowerCase();
function checkInside(item: T, term: string) {
if (
typeof item === 'string' &&
item.toString().toLowerCase().includes(toCompare)
) {
return true;
}
for (const property in item) {
if (item[property] === null || item[property] == undefined) {
continue;
}
if (typeof item[property] === 'object') {
if (checkInside(item[property] as T, term)) {
return true;
}
} else if (
item[property].toString().toLowerCase().includes(toCompare)
) {
return true;
}
}
return false;
}
return items.filter((item) => {
return checkInside(item, term);
});
}
}
ng2searchfilter omit
https://github.com/solodynamo/ng2-search-filter/issues/55#issuecomment-607651243
ng generate pipe CustomFilterPipe