2

我正在尝试按照此处的教程进行操作:

https://mdbootstrap.com/docs/angular/tables/datatables/

但问题是每当我尝试对分页做任何事情时都会出错。

无法读取未定义的属性“setMaxVisibleItemsNumberTo”

我的 HTML 看起来像这样:

<table class="table-striped table-hover z-depth-1" stickyHeader="true" #productsTable="mdbTable"
    mdbTable *ngIf="!loading && products && products.length">
    <thead class="sticky-top">
        <tr>
            <th *ngFor="let head of headers; let i = index" [mdbTableSort]="products"
                [sortBy]="headers[i]" scope="col">{{head | titlecase}} <mdb-icon fas icon="sort">
                </mdb-icon>
            </th>
        </tr>
    </thead>
    <tbody #row>
        <tr mdbTableCol *ngFor="let product of products; let i = index">
            <th *ngIf="i+1 >= mdbTablePagination.firstItemIndex && i < mdbTablePagination.lastItemIndex"
                scope="row">{{product.gtin}}</th>
            <td
                *ngIf="i+1 >= mdbTablePagination.firstItemIndex && i < mdbTablePagination.lastItemIndex">
                {{product.title}}</td>
            <td
                *ngIf="i+1 >= mdbTablePagination.firstItemIndex && i < mdbTablePagination.lastItemIndex">
                {{product.retailer}}</td>
            <td
                *ngIf="i+1 >= mdbTablePagination.firstItemIndex && i < mdbTablePagination.lastItemIndex">
                {{product.currency}}</td>
            <td
                *ngIf="i+1 >= mdbTablePagination.firstItemIndex && i < mdbTablePagination.lastItemIndex">
                {{product.price}}</td>
        </tr>
    </tbody>
    <tfoot class="grey lighten-5 w-100">
        <tr>
            <td colspan="4">
                <mdb-table-pagination paginationAlign="" [tableEl]="productsTable"
                    [searchDataSource]="products"></mdb-table-pagination>
            </td>
        </tr>
    </tfoot>
</table>

组件代码如下所示:

@ViewChild(MdbTableDirective, { static: true }) mdbTable: MdbTableDirective;
@ViewChild(MdbTablePaginationComponent, { static: true }) mdbTablePagination: MdbTablePaginationComponent;
@Input() feedId: number;
products: any[];

headers: any[] = ['gtin', 'title', 'retailer', 'currency', 'price'];
maxVisibleItems: number = 10;

constructor(private cdRef: ChangeDetectorRef, private feedService: FeedService) {}

ngOnInit() {        
    this.list();
}

ngAfterViewInit() {
    this.cdRef.detectChanges();
}

private list() {
    this.feedService.listProducts(this.feedId).subscribe(products => {
        this.mdbTablePagination.setMaxVisibleItemsNumberTo(this.maxVisibleItems);

        this.mdbTablePagination.calculateFirstItemIndex();
        this.mdbTablePagination.calculateLastItemIndex();

        this.products = products;

        this.mdbTable.setDataSource(this.products);
    });
}

有谁知道为什么它不起作用?

4

1 回答 1

-1

在 setDataSource 方法之后移动分页代码

this.feedService.listProducts(this.feedId).subscribe(products => {
  this.products = products;
  this.mdbTable.setDataSource(this.products);      

  // Move pagination code after setting datasource
  this.mdbTablePagination.setMaxVisibleItemsNumberTo(this.maxVisibleItems);
  this.mdbTablePagination.calculateFirstItemIndex();
  this.mdbTablePagination.calculateLastItemIndex();   
});
于 2019-09-18T00:09:57.583 回答