Last active
September 11, 2018 14:33
-
-
Save wholypantalones/ce221af6b99fb7e2b4dd121e721f7fcc to your computer and use it in GitHub Desktop.
Angular 4 search pipe for an array of objects
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// *ngFor="let row of (sample | searchPipe: input.value)" | |
@Pipe({ | |
name: 'searchPipe', | |
pure: false | |
}) | |
export class SearchPipe implements PipeTransform { | |
transform(sample: any[], searchTerm: string): any[] { | |
if (!searchTerm) return sample; | |
searchTerm = searchTerm.toLowerCase(); | |
if (sample) { | |
return sample.filter(obj => { | |
Object.keys(obj).forEach(key => { | |
obj[key] = obj[key].toString().toLowerCase(); | |
}); | |
return Object.keys(obj).some(key => obj[key].includes(searchTerm)); | |
}); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment