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
Does not work with Nested JSON Object #45
Labels
Comments
You are rigth, the scope of this pipe doesnt include nested objects, but you can make a small modification to make it recursive, for example: 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): any {
if (!term || !items) return items;
return Ng2DeepSearchPipe.filter(items, term);
}
/**
*
* @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): Array<{ [key: string]: any }> {
const toCompare = term.toLowerCase();
function checkInside(item: any, term: string) {
for (let property in item) {
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);
});
}
} Here a working example: https://stackblitz.com/edit/angular-t1dzgn-2bhkja |
@aVolpe Yes, did implement with custom pipe! But it's better if you add that in code by taking pull request! |
Yes! please make a PR! |
@aVolpe Will do it. Thanks! |
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
I have JSON object like:
So I can able to search the name
A
but If typedSatara
it does not matches the nested object (As per your pipe code from GitHub)Expected Result:
I should be able to see both records if I typed
Satara
Link to Reproduce:
https://stackblitz.com/edit/angular-t1dzgn
The text was updated successfully, but these errors were encountered: