mdb angular 数据表不支持分页
mdb angular datatable not working with pagination
我正在尝试按照此处的教程进行操作:
https://mdbootstrap.com/docs/angular/tables/datatables/
但问题是每当我尝试对分页做任何事情时都会出错。
Cannot read property 'setMaxVisibleItemsNumberTo' of undefined
我的 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);
});
}
有谁知道为什么它不起作用?
将分页代码移到 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();
});
我正在尝试按照此处的教程进行操作:
https://mdbootstrap.com/docs/angular/tables/datatables/
但问题是每当我尝试对分页做任何事情时都会出错。
Cannot read property 'setMaxVisibleItemsNumberTo' of undefined
我的 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);
});
}
有谁知道为什么它不起作用?
将分页代码移到 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();
});