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)
});
}
Angular Pipes Multiple custom pipesIt is possible to bundle all frequently used pipes in one Module and import that new module in any component needs the pipes.
The chaining Pipe is used to perform the multiple operations within the single expression. This chaining operation will be chained using the pipe (I). In the following example, to display the birthday in the upper case- will need to use the inbuilt date-pipe and upper-case-pipe.
Impure pipes in angular are the pipes that execute when it detects an impure change in the input value. An impure change is when the change detection cycle detects a change to composite objects, such as adding an element to the existing array.
You can easily pass multiple arguments in pipe in angular 6, angular 7, angular 8 and angular 9 application. In this example we will create 'descPipe' custom pipe and create dynamic description with multiple parameters. basically we will create "persons" array and print in table then using custom pipe that will create dynamic description.
It is how the named pipe looks like used in Angular components. { { salutation | name: ‘firstName’ : ‘middleName’ : ‘lastName’ }} PipeTransform contains the transform method. Here is a syntax value is an attribute which we are going to apply pipe operation args- are arguments passed with either single or multiple arguments
To create a custom pipe, we have to import Pipe and Pipe Transform from Angular/core. In the @Pipe directive, we have to give the name to our pipe, which will be used in our .html file.
Usually, pipe accepts single arguments, Multiple arguments can be passed to pipe with delimiter : separation. Suppose we have employee data coming from REST API and want to display the data in a database table.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With