Filter In Angular 4

Introduction

Filter is an important part in AngularJS as well as Angular 2 or Angular 4. It is basically used to filter an item from a group of items, which are there in an array or an object array. It selects a subset of the items from an array and returns it as a new array and this item is displayed on UI.
 
@angular/core of Angular 4 or 2 module library provides two moduels - Piple and PipeTransform for the filtering feature.
  1. import { Pipe, PipeTransform } from '@angular/core';  

Filter Code

Now, I will explain step by step by creating one filter.
 
Create one file MyFilterPiple.ts inside Filter folder and write the code, as shown below.
  1. import { Pipe, PipeTransform } from '@angular/core';  
  2. import { Birds } from '../models/birds';  
  3.   
  4. @Pipe({  
  5.     name: 'myfilter',  
  6.     pure: false  
  7. })  
  8.   
  9. export class MyFilterPipe implements PipeTransform {  
  10.     transform(items: any[], filter: Birds): any {  
  11.         if (!items || !filter) {  
  12.             return items;  
  13.         }  
  14.         return items.filter(item => item.name.indexOf(filter.name) !== -1);  
  15.     }  
  16. }  
Here, Filter name is myfilter and logic of filtering is written inside MyFilterPiple class.
{ Birds } is a model class, which contains the properties given below.
  1. export class Birds {  
  2.     id: number;  
  3.     name: string;  
  4. }  
Now, import MyFilterPiple class inside your app.module.ts class, as shown below.
  1. import { NgModule }       from '@angular/core';  
  2. import { BrowserModule }  from '@angular/platform-browser';  
  3. import { FormsModule }    from '@angular/forms';  
  4. import { HttpModule, JsonpModule } from '@angular/http';  
  5.   
  6. import { AppComponent } from './app.component';  
  7. import {MyFilterPipe} from './filter/MyFilterPipe';  
  8.   
  9. @NgModule({  
  10.   imports: [  
  11.     BrowserModule,  
  12.     FormsModule,  
  13.     HttpModule,  
  14.     JsonpModule  
  15.   ],  
  16.   declarations: [  
  17.     AppComponent,  
  18.     MyFilterPipe  
  19.   ],  
  20.   bootstrap: [ AppComponent ]  
  21. })  
  22. export class AppModule {  
  23. }  
Now, it's time to use Filter inside the component, as shown below.
  1. import { Component } from '@angular/core';  
  2. import {Birds} from '../models/birds';  
  3.   
  4. @Component({  
  5.   template: `  
  6.     <h2>Birds</h2>  
  7.     <p>List of Birds</p>  
  8.     <div>  
  9.       <ul>  
  10.        <li *ngFor="let bird of birds | myfilter:filterargs">  
  11.          {{bird.id}} {{bird.name}}  
  12.        </li>   
  13.       </ul>  
  14.     </div>  
  15.     `  
  16. })  
  17.   
  18. export class BirdListComponent {  
  19.   filterargs = {name: 'Eagle'};  
  20.   birds: Birds[] = [  
  21.   {id: 11, name: 'Crow'},  
  22.   {id: 12, name: 'Peacock'},  
  23.   {id: 13, name: 'Sparrow'},  
  24.   {id: 14, name: 'Cuckoo'},  
  25.   {id: 15, name: 'Eagle'},  
  26.   {id: 16, name: 'Swan'},  
  27.   {id: 17, name: 'Duck'},  
  28.   {id: 18, name: 'Hen'},  
  29.   {id: 19, name: 'Parrot'},  
  30.   {id: 20, name: 'Woodpecker'}  
  31.   ]  
  32. }   
Inside *ngFor, you need to put the name of the filter and the filter arguments i.e. the value, which you want to filter. Here, filtering the value is hardcoded. It should be dynamic and come via textbox. For basic understanding, I hardcoded it, as shown below.
  1. filterargs = {name: 'Eagle'};  
Complete filter line inside *ngFor, as shown below.
  1. <li *ngFor="let bird of birds | myfilter:filterargs">  
Hence, it will filter and give the subset of the bird name Eagle, which is displayed on UI.
 
Download and run the complete attached code. It is node.js, which must be installed into your PC.

Execute the project, as shown below.

Go to root folder "routingpractice"  ~~ Open the command prompt ~~ type "npm start" ~~ it will automatically load on the Browser.

Conclusion

Filter in Angular is a very powerful concept. It filters the value from an array or an object array and displays on UI. Angular 4 filter is very good and far better than AngularJS filter when it comes to performance.

X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now