Skip to content
This repository was archived by the owner on Jun 27, 2023. It is now read-only.

Commit 6d31561

Browse files
Added Support for nested object (Fix-45)
1 parent c90f546 commit 6d31561

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

src/ng2-filter.pipe.spec.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,9 @@ describe('Pipe: Default', () => {
2424
const list = [{ a: 'b' }, { a: 'c' }];
2525
expect(pipe.transform(list, 'b')).toEqual([{ a: 'b' }]);
2626
});
27+
28+
it('Filter the nested object', () => {
29+
const list = [{ a: 'e' }, { a: { b: 'd' , c: { b : 'd' }}}];
30+
expect(pipe.transform(list, 'd')).toEqual([{ a: { b: 'd', c: { b : 'd' }}}]);
31+
});
2732
});

src/ng2-filter.pipe.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ import { Pipe, PipeTransform, Injectable } from "@angular/core";
88
export class Ng2SearchPipe implements PipeTransform {
99

1010
/**
11-
* @param items object from array
12-
* @param term term's search
13-
*/
11+
* @param items object from array
12+
* @param term term's search
13+
*/
1414
transform(items: any, term: string): any {
1515
if (!term || !items) return items;
1616

@@ -27,16 +27,25 @@ export class Ng2SearchPipe implements PipeTransform {
2727

2828
const toCompare = term.toLowerCase();
2929

30-
return items.filter(function (item: any) {
30+
function checkInside(item: any, term: string) {
3131
for (let property in item) {
3232
if (item[property] === null || item[property] == undefined) {
3333
continue;
3434
}
35+
if (typeof item[property] === 'object') {
36+
if (checkInside(item[property], term)) {
37+
return true;
38+
}
39+
}
3540
if (item[property].toString().toLowerCase().includes(toCompare)) {
3641
return true;
3742
}
3843
}
3944
return false;
45+
}
46+
47+
return items.filter(function (item) {
48+
return checkInside(item, term);
4049
});
4150
}
4251
}

0 commit comments

Comments
 (0)