2017-06-01 46 views

回答

1

使用* ngFor在你的表中創建行和使用管道

<tr *ngFor="let item of items | filterPipe: searchToken"> 

添加一個搜索欄

<input placeholder="Type to filter" [(ngModel)]="searchToken"/> 

在組件添加變量searchToken

searchToken: string; 

添加自定義管道:

import { Pipe, PipeTransform } from '@angular/core'; 

@Pipe({ 
    name: 'filterPipe' 
}) 
export class FilterPipe implements PipeTransform { 

    transform(items: any[], searchToken: string) { 


     if (searchToken == null) 
      searchToken = ""; 


     searchToken = searchToken.toLowerCase(); 

     return items.filter(elem => elem.name.toLowerCase().indexOf(searchToken) > -1); 
    } 

} 

最後不要忘了把管道進入聲明中ngModule:

declarations: [ App, FilterPipe ] 
相關問題