Pass multiple values to pipe in Angular 6

  • A+
Category:Languages

I need to create a search form in Angular 6 with pipe and must pass multiple arguments to pipe .

nameSearch , emailSearch ,roleSeach , accountSearch

 <tr *ngFor="let user of data | searchuser: nameSearch" ></tr> 

and this my pipe :

@Pipe({   name: 'searchuser' }) export class SearchuserPipe implements PipeTransform {    transform(users: IUser[], nameSearch: string): IUser[] {     if(!users) return [];     if(!nameSearch) return users;      nameSearch=nameSearch.toLocaleLowerCase();     return users.filter(item=>       {          return item.desplayName.toLocaleLowerCase().includes(nameSearch)       });   } 

please guide me how create pipe search with multi argument .

Edit :

  transform(users: IUser[], nameSearch: string ,eamilSearch:string,roleSearch:string): IUser[] { if(!users) return []; if(!nameSearch) return users; if(!eamilSearch) return users; if(!roleSearch) return users;  nameSearch=nameSearch.toLocaleLowerCase(); return users.filter(item=>   {       item.desplayName.toLocaleLowerCase().includes(nameSearch),       item.email.toLocaleLowerCase().includes(eamilSearch),       item.description.toLocaleLowerCase().includes(roleSearch)             }); } 

 


You can add more parameters to the transform method that you'll have to implement in the pipe.

Make these parameters as optional so that you could use them as per your convenience.

Something like this:

import { Pipe, PipeTransform } from '@angular/core';  export interface IUser {   displayName: string;   name: string;   email: string;   role: string;   account: string;   description: string; }  @Pipe({   name: 'searchUser' }) export class SearchUserPipe implements PipeTransform {    transform(     users: IUser[],     nameSearch?: string,     emailSearch?: string,     roleSearch?: string,     accountSearch?: string   ): IUser[] {      if (!users) return [];     if (!nameSearch) return users;     nameSearch = nameSearch.toLocaleLowerCase();     users = [...users.filter(user => user.displayName.toLocaleLowerCase() ===  nameSearch)];      if (!emailSearch) return users;     emailSearch = emailSearch.toLocaleLowerCase();     users = [...users.filter(user => user.email.toLocaleLowerCase() ===  emailSearch)];      if (!roleSearch) return users;     roleSearch = roleSearch.toLocaleLowerCase();     users = [...users.filter(user => user.role.toLocaleLowerCase() ===  roleSearch)];      return users;   } } 

Now in your template, you can simply use this pipe and pass arguments separated by a color(:), something like this:

<input type="text" placeholder="name" [(ngModel)]="nameSearch"> <input type="text" placeholder="email" [(ngModel)]="emailSearch"> <input type="text" placeholder="role" [(ngModel)]="roleSearch"> <input type="text" placeholder="account" [(ngModel)]="accountSearch">  <table>   <tbody>     <tr *ngFor="let user of data | searchUser: nameSearch: emailSearch: roleSearch: accountSearch">       <td>         {{ user | json }}       </td>     </tr>   </tbody> </table> 

Here's also the Component Code:

import { Component } from '@angular/core';  @Component({...}) export class AppComponent  {    nameSearch = '';   emailSearch = '';   roleSearch = '';   accountSearch = '';    data = [...]; } 

Here's a Working Sample StackBlitz for your ref.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: