Angular 2+ Filter Pipes And Custom Filter Pipes

In this article, I am going to show you the predefined filter pipe usage as well as show you how we can create our own custom filter pipe and its usage.

In this article, I am going to show you the predefined filter pipe usage as well as show you how we can create our own custom filter pipe and its usage.

To get the usage of predefined filter pipe, we have to follow the below steps.

Step #1 Install ng2-filter-pipe package

We can install ng2-filter-pipe package either using bower or by using npm tool or with any other tools. Below is the command to install ng2-filter-pipe package using npm tool:

npm install ng2-filter-pipe

We can install the same package using bower  tool as well, below is the command to install the above package using bower tool:

bower install ng2-filter-pipe

Step #2 Add the filter module into AppModule.ts file

Import the below-highlighted package into AppModule.ts file and add that module into imports section of @NgModule, which is highlighted as below.

AppModule.ts

  1. import { NgModule } from '@angular/core';  
  2. import { BrowserModule  } from '@angular/platform-browser';  
  3. import { AppComponent } from './app';   
  4. import { Ng2FilterPipeModule } from 'ng2-filter-pipe';  
  5.    
  6. @NgModule({  
  7.   imports: [BrowserModule, Ng2FilterPipeModule],  
  8.   declarations: [AppComponent],  
  9.   bootstrap: [AppComponent]  
  10. })  
  11. export class AppModule {}  

Step #3 Use the filter pipe in your component

Once step #2 is done, use the filter pipe in your component. So, to use the filter pipe in our component, we have to create our own component. Find the below sample code to create a sample component.

app.component.ts

  1. import { Component } from '@angular/core';  
  2.    
  3. @Component({  
  4.   selector: 'ex-app',  
  5.   template: `  
  6.     <div>  
  7.         <input type="text" [(ngModel)]="userFilter.name" placeholder="name">  
  8.         <ul>  
  9.           <li *ngFor="let item of items | filterBy: itemFilter">{{ item.text }}</li>  
  10.             
  11.           <!-- if you want to show default message -->  
  12.           <li *ngIf="(items | filterBy: stringFilter).length === 0">No matches found</li>  
  13.         </ul>  
  14.     </div>    
  15.   `  
  16. })  
  17.    
  18. export class AppComponent {  
  19.  items: any[] = [{ name: 'Copy of 123456' }, { name: 'Copy of 1234568' }, { name: ''Copy of asd', ''Copy of asd1''Current Profile''1234 1234''123321122 1234551''123321122 1234551'}];  
  20.   itemFilter: any = { text: '' };  

Step #4 Compile and Run the application

You can compile and run the application by using the below commands using npm tool.

npm start

or

ng serve

or

ng s

or

npm test

Output

Angular

Step #5 Type some text in the search box

Type some text which is present in the list. For instance:

Ex #1 "asd"

Output

Angular

Ex #2 "1234"

Output

Angular

Let's consider that the above filter does not meet our requirement, so in such cases, we have to go with our own custom filter to meet the requirement.

Follow the below steps to create our own custom filter.

Step #1 Create a custom search filter pipe

Below is the sample code to create a custom filter pipe with name "search-filter.pipe.ts"  file, where you can write our own logic to meet the requirement. In the below code snippet I have created a custom pipe named "search-filter.pipe.ts" file which takes 2 inputs, first is the list of values and second is the search input. The below-mentioned code checks the index in the given list for the provided search input and returns the filtered list.

search-filter.pipe.ts

  1. import { Injectable, Pipe, PipeTransform } from '@angular/core';  
  2.   
  3. @Pipe({  
  4.     name: 'searchfilter'  
  5. })  
  6.   
  7. @Injectable()  
  8. export class SearchFilterPipe implements PipeTransform {  
  9.     transform(items: any[], value: string): any[] {  
  10.         if (!items) return [];  
  11.         if(value) {  
  12.             return items.filter(item => item.text.toLocaleLowerCase().indexOf(value.toLocaleLowerCase()) > -1);  
  13.         }  
  14.         else  
  15.         {  
  16.             return items;  
  17.         }  
  18.     }  

Step #2 Refer the component in the module file

Below is the code to create a module file and declare the above created component in the declaration section.

app.module.ts

  1. ------------------------  
  2. ------------------------  
  3. ------------------------  
  4. import { SearchFilterPipe } from './pipes/search-filter.pipe';  
  5.   
  6. @NgModule({  
  7.     imports: [  
  8.          ------------------------  
  9.          ------------------------  
  10.          ------------------------  
  11.         Ng2FilterPipeModule  
  12.     ],  
  13.     declarations: [  
  14.          ------------------------  
  15.          ------------------------  
  16.          ------------------------  
  17.         SearchFilterPipe  
  18.     ],  
  19.     providers: [  
  20.         ------------------------  
  21.         ------------------------  
  22.         ------------------------  
  23.     ],  
  24.     exports: [  
  25.         ------------------------  
  26.         ------------------------  
  27.         ------------------------  
  28.     ],  
  29.     entryComponents: [  
  30.         ------------------------  
  31.         ------------------------  
  32.         ------------------------  
  33.     ]  
  34. })  
  35. export class CoreModule {  
  36. }  

Step #3 Use the created custom filter in the component template file

In the below code snippet, I have used the created custom filter pipe which is highlighted as below.

app.component.ts

  1. import { Component } from '@angular/core';  
  2.    
  3. @Component({  
  4.   selector: 'ex-app',  
  5.   template: `  
  6.     <div>  
  7.         <input type="text" [(ngModel)]="userFilter.name" placeholder="name">  
  8.         <ul>  
  9.           <li *ngFor="let item of items | searchfilter: consentFilter.text">{{ item.text }}</li>  
  10.             
  11.           <!-- if you want to show default message -->  
  12.           <li *ngIf="(items | filterBy: stringFilter).length === 0">No matches found</li>  
  13.         </ul>  
  14.     </div>    
  15.   `  
  16. })  
  17.    
  18. export class AppComponent {  
  19.  items: any[] = [{ name: 'Copy of 123456' }, { name: 'Copy of 1234568' }, { name: ''Copy of asd', ''Copy of asd1''Current Profile''1234 1234''123321122 1234551''123321122 1234551'}];  
  20.   itemFilter: any = { text: '' };  
  21. }  

Step #3 Type some text in the search box

Type some text which is present in the list. For instance:

Ex #1 "asd"

Output

Angular

Ex #2 "1234"

Output

Angular

I appreciate your valuable feedback. Happy coding :)