Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass multiple values to pipe in Angular 6

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)          
  });
}
like image 558
Mr-Programer Avatar asked Nov 30 '18 09:11

Mr-Programer


People also ask

Can we use multiple pipes in Angular?

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.

What is chaining pipe in Angular?

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.

What is impure pipe in Angular?

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.

How to pass multiple arguments in pipe in Angular 6?

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.

What is pipetransform in Angular 2?

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

How to create a custom pipe in Angular 2?

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.

How to pass multiple arguments to a pipe in Python?

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.


Video Answer


1 Answers

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.

like image 183
SiddAjmera Avatar answered Oct 04 '22 04:10

SiddAjmera