Angular 2 Table Search Pipe Filter not working

本小妞迷上赌 提交于 2019-12-07 05:00:34

I guess you want it like this: https://plnkr.co/edit/olkIEiOc67Ld28NuASef?p=preview

You dont want to "hide" the whole td, instead you just want to "hide" the value..

@Pipe({
  name: 'searchPipe'
})
export class searchPipe implements PipeTransform {
  transform(value: any, args: string[]): any {
    let filter = args[0];

    if (filter && Array.isArray(value)) {
      let filterKeys = Object.keys(filter);
      return value.filter(item => filterKeys.reduce((key, keyName) => key && item[keyName].toUpperCase() === filter[keyName].toUpperCase(), true));
    }
    // new case !
    else if (filter && typeof value === 'string') {
      return value.toUpperCase().indexOf(filter.toUpperCase()) >= 0 ? value : '';
    }
    else {
      return value;
    }
  }
}
<td *ngFor="let rowValues of row | rowPipe">{{ rowValues | searchPipe : searchFilter.value }}</td>

UPDATE

To show the whole row, you have to filter your tableData.

Change your pipe like this:

@Pipe({
  name: 'searchPipe'
})
export class searchPipe implements PipeTransform {
  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));
    }
  }
}
<tr *ngFor="let row of tableData | searchPipe : searchFilter.value">
  <td *ngFor="let rowValues of row | rowPipe">{{ rowValues }}</td>
</tr>

live demo: https://plnkr.co/edit/jXfqfCuJpKdw9HtL569T?p=preview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!