2017-08-07 51 views
0

我有一個管道過濾器,它通過name過濾我的客戶端列表。我需要添加其他過濾器,使客戶可以通過account_number被過濾管道過濾器:使用管道過濾器,使其在多個條件下工作

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

@Pipe({ 
    name: 'filter', 
    pure: false 
}) 

export class FilterPipe implements PipeTransform { 

    transform(items: any[], term): any { 
    return term ? items.filter(item => item.name.toLowerCase().indexOf(term.toLowerCase()) != -1) : items; 
    } 

} 

在我看來,我的過濾器綁定的方式是這樣的:

<input type="text" class="form-control" placeholder="Name" name="term" [(ngModel)]="term"> 
<input type="number" class="form-control" placeholder="Account number"> <!--Here I want to make another ngModel--> 

<table> 
    <tr *ngFor="let client of clients | filter: term | sortBy: 'id'; let i = index" 
    <td>{{client.name}}</td> 
    <td>{{client.account_number}}</td> 
    </tr> 
</table> 

所以,做我需要爲另一個屬性過濾另一個pipe,或者可以在一個pipe中完成嗎?另外,如果它是兩個不同的管道,我應該如何將它包含在視圖中的過濾器中?謝謝。

回答

1

你可以用單管做嘗試這種

transform(values: any[], filter: string): any { 
    if (!values || !values.length) return []; 
    if (!filter) return values; 

    filter = filter.toUpperCase(); 

    if (filter && Array.isArray(values)) { 
     const keys = Object.keys(values[0]); 
     return values.filter(v => v && keys.some(k => v[k].toUpperCase().indexOf(filter) >= 0)); 
    } 
    } 
+0

不幸的是,我發現了一個錯誤:'錯誤類型錯誤:v [K] .toUpperCase不是function' –

+0

你有什麼用繩子嘗試或號碼? –

+0

當使用數字或字符串獲取錯誤時,我嘗試了兩種變體 –