我创建了自己的管道,用于过滤表中的任何字段。当我将字符串放入搜索框中时,它会在我的控制台中正确打印“找到”,但表格中没有显示任何行。当我完全移除管道时,一切正常,表包含所有记录。
import { Component, Input, OnInit } from '@angular/core';
import { MyTableData } from '../interfaces/my-table-data.interface'
@Component({
selector: 'my-table',
template: `
<div style="width: 100%">
<div style="float: left; height: 50px; width: 100%">
Search: <input type="text" [(ngModel)]="filterValue" style="height: 30px; border: 1px solid silver"/>
</div>
<div style="float: left; width: 100%">
<table>
<tr *ngFor="let row of data.rows | myTableFilter:filterValue">
<td *ngFor="let value of row">{{value}}</td>
</tr>
</table>
</div>
</div>
`
})
export class MyTableComponent implements OnInit {
@Input() data: MyTableData;
filterValue: string;
ngOnInit() {
}
}
和管道:
import { Component, Injectable, Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'myTableFilter',
pure: false
})
@Injectable()
export class MyTableFilterPipe implements PipeTransform {
transform(items: any[], arg: string): any {
if(arg == '') return true;
items.filter(item => {
for(let value of item) {
if(String(value).indexOf(arg) !== -1) {
console.log('found match') //it prints correctly
return true;
}
}
return false;
});
};
}
问候