如何在ngx分页中制作过滤器

时间:2019-03-19 21:58:07

标签: javascript angular pagination

在Angular 7 + Electron 4中,我使用ngx-pagination,但无法解决滤镜问题。
我在documentation中进行操作,但出现错误未捕获的错误:模板解析错误: 找不到管道'stringFilter'
请帮帮我。预先感谢
HTML-1:

<input
  type="text"
  name="search"
  class="search__input"
  placeholder="Search by Name..."
  [(ngModel)]="tableService.filter"> 

Html-2:

<ul class="table-body__list">
 <li *ngFor="let item of tableService.items | stringFilter: tableService.filter | paginate: config">
   <app-item [item]="item"></app-item>
 </li>
</ul>

<pagination-controls
  [maxSize]="maxSize"
  directionLinks="true"
  responsive="true"
  previousLabel="Previous page"
  nextLabel="Next page"
  (pageChange)="onPageChange($event)">        
</pagination-controls>

TypeScript:

import { Component, OnInit } from '@angular/core';
import { PaginationInstance } from 'ngx-pagination';
import { TableService } from '../../services/table.service';
@Component({
  selector: 'app-jobs-table',
  templateUrl: './jobs-table.component.html',
  styleUrls: ['./jobs-table.component.scss']
})
export class JobsTableComponent implements OnInit {
  filter = '';
  maxSize = 9;
  config: PaginationInstance = {
    itemsPerPage: 11,
    currentPage: 1
  };

  constructor(public tableService: TableService) { }

  ngOnInit() {
  }

  onPageChange(number: number) {
    this.config.currentPage = number;
  }

}

在TableService中:

filter = '';

2 个答案:

答案 0 :(得分:1)

在github上找到(在存储库中的过滤器上搜索)。显然npx-pagination没有任何标准的过滤器管道。他们的文档是....次优

import {Pipe} from "@angular/core";

/**
 * A simple string filter, since Angular does not yet have a filter pipe built in.
 */
@Pipe({
    name: 'stringFilter'
})
export class StringFilterPipe {

    transform(value: string[], q: string) {
        if (!q || q === '') {
            return value;
        }
        return value.filter(item => -1 < item.toLowerCase().indexOf(q.toLowerCase()));
    }
}

有趣的评论:由于性能原因,Angular删除了用于过滤的管道。

答案 1 :(得分:0)

您只能使用filter更改stringFilter。

<ul class="table-body__list">
 <li *ngFor="let item of tableService.items | filter: tableService.filter | paginate: config">
   <app-item [item]="item"></app-item>
 </li>
</ul>