This repository has been archived by the owner on Jun 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 40
How can I ignore a Property with Filter? #40
Comments
@Jonathan002 Currently this feature is not implemented but you can create your own custom pipe to achieve the same. Custom Pipe (Create Angular Pipe): import { Pipe, PipeTransform, Injectable } from "@angular/core";
@Pipe({
name: 'deepFilter',
pure: false
})
@Injectable()
export class Ng2DeepSearchPipe implements PipeTransform {
/**
* @param items object from array
* @param term term's search
*/
transform(items: any, term: string, props?: Array<string>): any {
if (!term || !items) return items;
return Ng2DeepSearchPipe.filter(items, term, props);
}
/**
*
* @param items List of items to filter
* @param term a string term to compare with every property of the list
*
*/
static filter(items: Array<{ [key: string]: any }>, term: string, columnsToExclude?: Array<string>): Array<{ [key: string]: any }> {
const toCompare = term.toLowerCase();
function checkInside(item: any, term: string) {
for (let property in item) {
if (columnsToExclude.some(x=> x === property)) {
continue;
}
else {
if (item[property] === null || item[property] == undefined) {
continue;
}
if (typeof item[property] === 'object') {
if (checkInside(item[property], term)) {
return true;
}
}
if (item[property].toString().toLowerCase().includes(toCompare)) {
return true;
}
}
}
return false;
}
return items.filter(function (item) {
return checkInside(item, term);
});
}
} HTML Code:
Here is a working demo: |
@aVolpe Do we need this feature? I mean is this required? |
@prashantpimpale93 maybe with the recent change to allow nested json this is needed. Can you make a PR for this? I only suggest changing the |
@aVolpe yes will make a PR! |
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
How can I get the search filter to be selective about the object properties?
Example:
Data being iterated over:
Goal is to only have the searchFilter respond to name searches
The text was updated successfully, but these errors were encountered: