In the demo project product management create a new file and name it as
product-filter.pipe.ts, and use the below code.
product-filter.pipe.ts
[c]
import { PipeTransform, Pipe } from '@angular/core';
import { IProduct } from './product';
@Pipe({
name: 'productFilter'
})
export class ProductFilterPipe implements PipeTransform{
transform(value: IProduct[], filterBy: string): IProduct[] {
filterBy = filterBy ? filterBy.toLocaleLowerCase() : null;
return filterBy ? value.filter((product: IProduct) =>
product.productName.toLocaleLowerCase().indexOf(filterBy) !== -1) : value;
}
}
[/c]
In the above code,
export keyword is used to import the pipe where ever needed and the class with the name product filter pipe defined and pipe transform interface is implemented.
The Pipe transform interface is imported from Angular core in the first line of the code. The method is defined for the pipe transform interface using the keyword
transform, inside the method the value parameter is defined to be an array of Iproduct and is been imported as shown in the second line of the code.
The class is decorated using the pipe decorator and the pipe name is specified as
productFilter and the pipe is added to the import statement in the first line of the code.
The
transform method is implemented using the JavaScript code, the goal is to transform the passed list of products to a filtered list based on a user-entered filter string. This first line of code uses a JavaScript conditional operator to handle the possibility that no
filterBy string was defined. If there is a filterBy, this code converts it to lowercase. So one can compare apples to apples when filter the product list. A case insensitive comparison is needed. If there is no filterBy, this code sets the filterBy to null. Then return the filtered list of products.
This code also uses the JavaScript conditional operator. If the filter variable has a true value, meaning that it is defined and not null, then the list is filterd. Otherwise, there was no filter specified and one can return the original value, which is the full list of products. Look closer at this filter statement. The array filter method is used to create a new array with elements that pass the test defined in the provided function. Use the ES 2015 arrow syntax to define that filter function. For each product in the list, the product name is converted to lowercase and the IndexOf is used to determine if the filtered text is found in the product name. If so, the element is added to the filtered list.
Now use the pipe in the product list component template. In the
ngfor class, add the pipe character and the name of the pipe,
productFilter. Be sure to insert within the quotes and also pass in the pipe argument. In this example, the listfilter property is passed. That property contains the value the user entered in the filterBy input box. The code below explain using the created pipe.
product-list.component.html
[html]
<div class='panel panel-primary'>
<div class='panel-heading'>
{{pageTitle}}
</div>
<div class='panel-body'>
<div class='row'>
<div class='col-md-2'>Filter by:</div>
<div class='col-md-4'>
<input type='text'
[(ngModel)]='listFilter' />
</div>
</div>
<div class ='row'>
<div class='col-md-6'>
<h3>Filtered by: {{listFilter}} </h3>
</div>
</div>
<div class='table-responsive'>
<table class='table'
*ngIf='products && products.length'>
<thead>
<tr>
<th>
<button class='btn btn-success'
(click)='toggleImage()'>
{{showImage ? 'Hide' : 'Show'}} Image
</button>
</th>
<th>Product</th>
<th>Code</th>
<th>Available</th>
<th>Price</th>
<th>5 Star Rating</th>
</tr>
</thead>
<tbody>
<tr *ngFor='let product of products | productFilter:listFilter'>
<td>
<img *ngIf='showImage'
[src]='product.imageUrl'
='product.productName'
[style.width.px]='imageWidth'
[style.margin.px]='imageMargin'>
</td>
<td>{{ product.productName }}</td>
<td>{{ product.productCode | lowercase }}</td>
<td>{{ product.releaseDate }}</td>
<td>{{ product.price | currency:'USD':true:'1.2-2'}}</td>
<td>{{ product.starRating }}</td>
</tr>
</tbody>
</table>
</div>
</div>
[/html]
Now, one need to tell the angular about the newly created pipe which can be done by declaring the pipe in an Angular module, open the Angular module which declare the product list component which is the
app module in this example. Add
ProductFilterPipe in the declaration array an import the
ProductFilterPipe as shown in the code below.
app.module.ts
[c]
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { AppComponent } from './app.component';
import { ProductListComponent } from './products/product-list.component';
import { ProductFilterPipe } from './products/product-filter.pipe';
@NgModule({
imports: [
BrowserModule,
FormsModule
],
declarations: [
AppComponent,
ProductListComponent,
ProductFilterPipe
],
bootstrap: [ AppComponent ]
})
export class AppModule { }
[/c]