角度:Paginator显示数组中的所有元素,而不是[pageSize]属性中声明的数字

时间:2018-12-23 17:39:54

标签: angular angular-material paginator

就像我在标题中写道的那样,分页器显示了dataSource.product数组中的所有数据,但是它应该只显示在[pageSize]属性中声明的元素数。我在下面放了.html和datasource.ts文件。

当我从mat-table属性 [dataSource]="dataSource.product" 中删除并仅保留*[dataSource]="dataSource"*时,它可以正常工作,但是我会收到错误消息TypeError: Cannot read property 'length' of undefined。此错误包含[dataSource]="dataSource"参数。

table.component.html

<div>
  <button (click)="goToProductAdd()" class="btn btn-success">Dodaj nowy produkt</button>
  <button class="btn btn-primary">Zarządzaj kategoriami produktów</button>
</div>

<div class="product-table">

  <mat-table #table [dataSource]="dataSource.product" matSort aria-label="Elements">

    <ng-container matColumnDef="id">
      <mat-header-cell *matHeaderCellDef mat-sort-header> Id </mat-header-cell>
      <mat-cell *matCellDef="let product"> {{product.id}} </mat-cell>
    </ng-container>

    <ng-container matColumnDef="name">
      <mat-header-cell *matHeaderCellDef mat-sort-header> Nazwa produktu </mat-header-cell>
      <mat-cell *matCellDef="let product"> {{product.name}} </mat-cell>
    </ng-container>

    <ng-container matColumnDef="price">
      <mat-header-cell *matHeaderCellDef mat-sort-header> Cena (zł)</mat-header-cell>
      <mat-cell *matCellDef="let product">{{product.price | number:'1.2-2'}}</mat-cell>
    </ng-container>

    <ng-container matColumnDef="category">
      <mat-header-cell *matHeaderCellDef mat-sort-header> Rodzaj produktu</mat-header-cell>
      <mat-cell *matCellDef="let product"> {{product.category}} </mat-cell>
    </ng-container>

    <mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
    <mat-row *matRowDef="let product; columns: displayedColumns;" (click)="goToProductDetail(product.id)"></mat-row>

  </mat-table>

  <mat-paginator #paginator
      [length]="dataSource?.product?.length"
      [pageIndex]="0"
      [pageSize]="30"
      [pageSizeOptions]="[30, 60, 90, 120]">
  </mat-paginator>
</div>

table-datasource.ts

import { DataSource } from '@angular/cdk/collections';
import { MatPaginator, MatSort } from '@angular/material';
import { map } from 'rxjs/operators';
import { Observable, of as observableOf, merge } from 'rxjs';
import {ProductService} from '../../service/product.service';

export interface ProductI {
  id: number;
  category: string;
  description: string;
  name: string;
  price: number;
  productType: string;
}

export class ProductTableDataSource extends DataSource<any> {

  product: ProductI[] = [];

  constructor(private paginator: MatPaginator, private sort: MatSort, private productService: ProductService) {
    super();
    this.productService.getAllProducts()
      .subscribe(value => this.product = value);
  }

  connect(): Observable<ProductI[]> {
    const dataMutations = [
      observableOf(this.product),
      this.paginator.page,
      this.sort.sortChange
    ];

    this.paginator.length = this.product.length;

    return merge(...dataMutations).pipe(map(() => {
      return this.getPagedData(this.getSortedData([...this.product]));
    }));
  }

  disconnect() {}

  private getPagedData(data: ProductI[]) {
    const startIndex = this.paginator.pageIndex * this.paginator.pageSize;
    return data.splice(startIndex, this.paginator.pageSize);
  }

  private getSortedData(data: ProductI[]) {
    if (!this.sort.active || this.sort.direction === '') {
      return data;
    }

    return data.sort((a, b) => {
      const isAsc = this.sort.direction === 'asc';
      switch (this.sort.active) {
        case 'category': return compare(a.category, b.category, isAsc);
        case 'name': return compare(a.name, b.name, isAsc);
        case 'id': return compare(+a.id, +b.id, isAsc);
        case 'price': return compare(+a.price, +b.price, isAsc);
        default: return 0;
      }
    });
  }
}

function compare(a, b, isAsc) {
  return (a < b ? -1 : 1) * (isAsc ? 1 : -1);
}

1 个答案:

答案 0 :(得分:0)

您只需返回到[dataSource]="dataSource"mat-paginator中即可:

<mat-paginator #paginator
  [length]="dataSource?.length"
  [pageIndex]="0"
  [pageSize]="30"
  [pageSizeOptions]="[30, 60, 90, 120]">
</mat-paginator>