Angular - Creating Custom Pipe(|)

In my previous article, I have discussed inbuilt pipes provided by Angular to transform the data.

But sometimes, to fulfill our requirement, we need to create our own pipe, i.e., a custom pipe. So, in this article, I am going to talk about:

  • How to create a custom pipe in Angular

As we already know that Pipes (|) in Angular are used to transform the data before displaying in a browser, so in this article, my requirement is to filter the student's record based on the value, i.e., name of the student entered in the search textbox given at the top.

See the below screen.

output

Once the user enters anything in the textbox, the student record will get filtered based on the name of the student. Please note that I have used bootstrap classes to apply the basic styles.

To build the above screen, I have used only one component (AppComponent) and Respective HTML template file. To create the custom pipe, I have created one TypeScript file and implemented PipeTransforms interface and implemented the custom logic inside the transform method.

Step 1 - Create the custom pipe

To create custom pipe, create one .ts file and paste the below code.

  1. import { Pipe } from "@angular/core";  
  2. import { PipeTransform } from "@angular/core";  
  3.   
  4. @Pipe({  
  5.     name: 'namefilterpipe' //name of the pipe  
  6.   
  7. })  
  8. // This custom pipe implements the PipeTransform interface  
  9. // as you can see in below code I have provided the custom implementation to the transform method to filter the record   
  10. //based on the character typed in the search textbox  
  11. export class NamePipe implements PipeTransform {  
  12.     transform(studentList: any, searchText: any) {  
  13.         if (searchText)  
  14.             return studentList.filter(x => x.Name.toLowerCase().startsWith(searchText.toLowerCase());  
  15.               
  16.         return studentList;  
  17.     }  
  18.   
  19. }  

As you can see in the above code, I have implemented Pipetransform interface and provided the body to transform method and in the decorator, i.e, @Pipe, I have given the name of the pipe as namefilterpipe. The transform method takes two arguments as we are applying a filter on the student list. The first argument will be the student list and the second argument will be our textbox value which a user will type. And based on the input values, I am filtering the student’s record and returning it.

After creating the file, declare that pipe inside the declaration section of the AppModule.

Step 2

Open the app.commponent.ts file and paste the following code.

  1. import { Component } from '@angular/core'  
  2. @Component({  
  3.     selector: 'my-app',  
  4.     templateUrl: './app/app.component.html',  
  5. })  
  6.   
  7. export class AppComponent {  
  8.   
  9.     public students: any;// this variable holds the list of students  
  10.     public searchText: any;  
  11.   
  12.     constructor() {  
  13.         this.students = [  
  14.             { Id: 1, Name: "Mahesh", Address: "Thane", Gender: "Male" },  
  15.             { Id: 2, Name: "Nishikant", Address: "Chembur", Gender: "Male" },  
  16.             { Id: 3, Name: "Sameer", Address: "Mulund", Gender: "Male" },  
  17.             { Id: 4, Name: "Nitin", Address: "Nahur", Gender: "Male" },  
  18.             { Id: 4, Name: "Siri", Address: "Worli", Gender: "Female" }  
  19.         ];  
  20.     }  
  21. }  

As you can see, in the above code, I have just created one student's array and I am initializing this array inside constructor.

Step  3

Now, open the app.component.html and paste the following code.

  1. <div class="container">  
  2.         <b>Enter Name :</b> <input type="text" [(ngModel)]="searchText"><br/>  
  3.         <table class="table table-sm">  
  4.             <thead>  
  5.                 <tr>  
  6.                     <th scope="col">Id</th>  
  7.                     <th scope="col">Name</th>  
  8.                     <th scope="col">Address</th>  
  9.                     <th scope="col">Gender</th>  
  10.                 </tr>  
  11.             </thead>  
  12.             <tbody>  
  13.                 <!--using *ngFor directive to iterate over student list-->  
  14.                 <tr *ngFor="let student of students | namefilterpipe : searchText">  
  15.                     <th scope="row">{{student.Id}}</th>  
  16.                     <td>{{student.Name }}</td>  
  17.                     <td>{{student.Address}}</td>  
  18.                     <td>{{student.Gender}}</td>  
  19.                 </tr>  
  20.             </tbody>  
  21.         </table>  
  22.     </div>  

Here,  I am displaying the students in the table using *ngFor structural directive and at the same time, I have applied the custom filter; i.e namefilterpipe, and passing the entered value as a parameter.

See the following code snippet.

  1. <tr *ngFor="let student of students | namefilterpipe : searchText">  

Run the code and see the below output screen.

output

In this way, we can create and use custom pipes. I hope this will help you someday. 

Thanks!

You can download the complete code from my GitHub repository.


Similar Articles