【问题标题】:How to use search pipe with pagination in ngFor using angular 4如何使用 Angular 4 在 ngFor 中使用带有分页的搜索管道
【发布时间】:2018-06-15 16:41:10
【问题描述】:

我有下面的搜索管道代码

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

@Pipe({
  name: 'pkhospitalparam'
})
export class PkhospitalparamPipe implements PipeTransform {

  transform(value: any[], searchString: string ) {

    if (!searchString) {
      console.log('no search');
      return value;
    }

    return value.filter(it => {
        const hospitalName = it.name.toLowerCase().includes(searchString.toLowerCase());

        return (hospitalName);
    });
 }

}

HTML:

 <input type="text" class="form-control" [(ngModel)]="searchHospitalParam"  />

             <div *ngFor="let result of resultArray | paginate: { itemsPerPage: 5, currentPage: p } | pkhospitalparam:searchHospitalParam"></div>

我已经在 ngFor 中传递了管道参数,它工作正常。问题是因为我已经实现了分页(大约 50 页),所以搜索不会发生 50 页。仅在我所在的页面上搜索过滤器。无论如何都要对所有页面进行全局搜索吗?

【问题讨论】:

  • 我们也能看到分页管道吗?
  • 在对数据进行分页和过滤时,您应该先过滤数据,然后再对其进行分页,而不是相反。因为在您对其进行分页后,您手中并没有完整的数据来过滤它。
  • 太棒了!它奏效了

标签: angular search pagination pipe


【解决方案1】:

这已经很老了,但可能对其他人有帮助,

通过使用 w-ng5 组件为我工作

1) 通过 npm 安装

npm install w-ng5 --save

2) 在 app.module.ts 中安装导入模块后

import { PipesModule } from 'w-ng5';

3) 在 app.module.ts 的导入部分添加

imports: [
    PipesModule,
 ]

4) 现在您可以使用过滤器进行过滤了

<input type="text" class="form-control" [(ngModel)]="searchHospitalParam"/>
   <div *ngFor="let result of resultArray | filter:searchHospitalParam" | paginate: { itemsPerPage: 5, currentPage: p } | ></div>

*NB 你应该在分页之前先过滤

【讨论】:

  • 您好,我遇到了一个小错误,因为当我使用w-ng5 时,它没有正确过滤,只显示一个空白列表,您能帮我解决这个问题吗?
  • 你能发布一个新问题来描述你遇到的问题
  • 嘿兄弟,这是question of my problem,问候!
猜你喜欢
  • 1970-01-01
  • 2017-09-09
  • 1970-01-01
  • 2017-10-07
  • 2020-04-01
  • 1970-01-01
  • 2019-05-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多