Last active
July 10, 2019 18:18
-
-
Save jamlfy/3bb60ad58de6d8a23f98d69e53a0690d to your computer and use it in GitHub Desktop.
Search on pipe in Angular2
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
import { Pipe, PipeTransform, Injectable } from '@angular/core'; | |
/** | |
* | |
* <input [(model)]="query" type="text" /> | |
* <ul> | |
* <li *ngFor="let hero of heroes | search:query" >{{hero.name}}</li> | |
* </ul> | |
*/ | |
@Pipe({ | |
name: 'search', | |
pure: false | |
}) | |
@Injectable() | |
export class search implements PipeTransform { | |
transform(items:any[], args:any):any[] { | |
var isSearch = (data:any): bool => { | |
var isAll = false; | |
if(typeof data === 'object' ){ | |
for (var z in data) { | |
if(isAll = isSearch(data[z]) ){ | |
break; | |
} | |
} | |
} else { | |
if(typeof args === 'number'){ | |
isAll = data === args; | |
} else { | |
isAll = data.toString().match( new RegExp(args, 'i') ); | |
} | |
} | |
return isAll; | |
}; | |
return items.filter(isSearch); | |
} | |
} |
Great Pipe! Working perfectly.
Thank you for the pipe ! ๐
Thank you !
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Two nitpicks:
[(ngModel)]
instead of[(model)]
boolean
instead ofbool
at line 18.Other than that, good gist ๐