2017-10-19 188 views
0

我有一个管道过滤:如何获得过滤(带管道)列表?

@Pipe({ 
name: 'filter' 
}) 
export class FilterPipe implements PipeTransform { 
transform(items: Array<any>, filter: { [key: string]: any }): Array<any> { 
    return items.filter(item => { 
     let notMatchingField = Object.keys(filter) 
      .find(key => item[key] !== filter[key]); 
     return !notMatchingField; // true if matches all fields 
    }); 
} 


} 

,我过滤我的名单中anotherpage.html:

<tr *ngFor="let item of _FilteredList | filter:peopleFilter" >

在anotherpage.ts我发现peopleFilter的上下文:

this.peopleFilter = { NAME: 'Gülcan' }; 

所以我运行它,我得到所有与名称对象是'Gülcan'在我的表中。但我写在我的另一页:console.log(this._FilteredList)我看到列表中的所有项目。无论如何只能看到那些过滤的物品?

+0

强烈建议您不要使用管道进行过滤和排序为这里讨论:https://angular.io/guide/pipes#no -filter-pipe相反,在组件类中进行筛选。我在这里有一个例子:https://blogs.msmvps.com/deborahk/filtering-in-angular/ – DeborahK

回答

0

试试这个代码为过滤器。它为我的作品:

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

@Pipe({ 
    name: 'filterPipe' 
}) 
export class FilterPipe implements PipeTransform { 
    transform(data: any[], keys: string[], filter: string) { 
     if (!filter || !keys || keys.length <= 0) { 
      return data; 
     } else { 
      return data.filter((elem) => { 
       var matched = false; 
       for (let key of keys) { 
        matched = matched || elem[key].toLocaleLowerCase().indexOf(filter.toLocaleLowerCase()) !== -1 
        if (matched) { 
         return true; 
        } 
       } 
       return matched; 
      }); 
     } 
    } 
} 

UPDATE HTML:

<tr *ngFor="let item of (_FilteredList | filterPipe:peopleFilter)"> 
+0

它不起作用。 –

+0

对不起,忘了HTML。更新之前更新 –

+0

有错误现在错误消失。但它现在不过滤 –

1

过滤器不改变源阵列。它产生另一个数组,然后在模板中呈现。目前无法访问它,但另一方面,我不认为这是真的有必要。如果您需要在代码中使用过滤数组,只需将它放在模板中而不是原始模板中即可。

所以,你要的是这样的:

app.component.ts

import {Component} from '@angular/core'; 
import {AppFilter} from './filter.pipe'; 

@Component({ 
    selector: 'app-root', 
    templateUrl: './app.component.html', 
    styleUrls: ['./app.component.css'] 
}) 
export class AppComponent { 
    items: any[] = []; 
    filteredItems: any[] = []; 
    private _filterId: string; 
    get filterId(): string { 
     return this._filterId; 
    } 
    set filterId(val: string) { 
     this._filterId = val; 
     this.filteredItems = this.filter.transform(this.items, {id: val}); 
    } 
    constructor(private filter: AppFilter) { 
     this.items = [ 
      { 
       id: '1', 
       text: 'item 1' 
      }, 
      { 
       id: '2', 
       text: 'item 2' 
      } 
     ]; 
     this.filterId = '1'; 
    } 
} 

app.component.html

<div> 
    <input type="text" [(ngModel)]="filterId" /> 
    <div *ngFor="let item of filteredItems">{{item.text}}</div> 
</div> 

app.module .ts

import {BrowserModule} from '@angular/platform-browser'; 
import {NgModule} from '@angular/core'; 

import {AppComponent} from './app.component'; 
import {FormsModule, ReactiveFormsModule} from '@angular/forms'; 
import {AppFilter} from './filter.pipe'; 

@NgModule({ 
    declarations: [ 
     AppComponent, 
     AppFilter 
    ], 
    imports: [ 
     BrowserModule, 
     FormsModule, 
     ReactiveFormsModule 
    ], 
    providers: [AppFilter], // <--- !!! it must be provided by some component or module 
    bootstrap: [AppComponent] 
}) 
export class AppModule { 
} 

filter.pipe.ts

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

@Pipe({ 
    name: 'filter' 
}) 
export class AppFilter implements PipeTransform { 
    transform(value: any, filter: {[key: string]: any}): any { 
     if (!value || !filter) { 
      return value; 
     } 
     return value.filter(item => { 
      return !Object.keys(filter).find(key => filter[key] && (item[key] !== filter[key])); 
     }); 
    } 
}