如何在垫板卡上使用角材料分页? [英] How to use angular-material pagination with mat-card?
问题描述
我想使用 mat-card 指令来展示我的产品.角度材料示例1 ,示例2 )
I want to use mat-card directive for show my products.The angular-material docs seems to me not thorough. I found many examples on the Internet with using Table with dataSource ( example 1, example 2 )
现在,我获得了所有产品的 productList ,并使用 ngFor 对其进行了迭代.我在页面上显示了所有产品.如何将 productList 提供给分页器并使用处理过的数据( paginationList )进行迭代.
Now I get the productList with all products and iterate it with ngFor. I show all products on the page. How can I feed the productList to the paginator and iterate with processed data ( paginationList ).
* component.html文件显示所有产品:
*component.html file it show all products:
<mat-paginator #paginator
[length]="productList.length"
[pageSize]="5"
[pageSizeOptions]="[5, 10, 25, 100]"
[showFirstLastButtons]="true"
</mat-paginator>
<ng-container *ngIf="productList.length; else notHeaveProducts">
<mat-card class="product-card" *ngFor="let product of productList">
<mat-card-header>
<mat-card-title>
<h3>{{ product.title }}</h3>
</mat-card-title>
</mat-card-header>
<img mat-card-image [src]="product.img_url" [alt]="product.title" [title]="product.title">
<mat-card-content>
<p>{{ product.description }}</p>
</mat-card-content>
<mat-card-actions>
<button mat-raised-button color="accent" (click)="addItemToCard(product)">Add to card</button>
</mat-card-actions>
</mat-card>
</ng-container>
* component.ts
*component.ts
export class ListComponent implements OnInit, OnDestroy {
public productList: Product[] = [];
public paginationList: Product[] = [];
ngOnInit() {
// I receive the products
this.activatedRoute.params.subscribe((params: any) => {
this.catalogService.getProductList()
.do((products: any) => {
this.productList = products;
})
.subscribe();
}
}
}
推荐答案
我有完全相同的要求,我使用了 mat-paginator和带有mat-grid-list包含mat-cards的来做到这一点.我使用了mat-grid-list来使列表具有响应性,以便可以调整编号.根据屏幕尺寸连续排列的元素数.这是我所做的:
I had the exact same requirement and I did this using mat-paginator with mat-grid-list containing the mat-cards. I used mat-grid-list to make my list responsive so that it can adjust the no. of elements in a row according to screen size. Here is what I did:
<mat-paginator [length]="length"
[pageSize]="pageSize"
[pageSizeOptions]="pageSizeOptions"
(page)="pageEvent = OnPageChange($event)">
</mat-paginator>
<mat-grid-list [cols]="breakpoint" rowHeight="4:5" (window:resize)="onResize($event)" >
<mat-grid-tile *ngFor="let product of pagedList">
<div>
<mat-card class="example-card">
mat-card content here..
</mat-card>
</div>
</mat-grid-tile>
</mat-grid-list>
这是组件的样子:
productsList: Product[]= [];
pagedList: Product[]= [];
breakpoint: number = 3; //to adjust to screen
// MatPaginator Inputs
length: number = 0;
pageSize: number = 3; //displaying three cards each row
pageSizeOptions: number[] = [3, 6, 9, 12];
ngOnInit() {
this.breakpoint = (window.innerWidth <= 800) ? 1 : 3;
this.productsList = <GetOrInitializeYourListHere>;
this.pagedList = this.productsList.slice(0, 3);
this.length = this.productsList.length;
});
}
OnPageChange(event: PageEvent){
let startIndex = event.pageIndex * event.pageSize;
let endIndex = startIndex + event.pageSize;
if(endIndex > this.length){
endIndex = this.length;
}
this.pagedList = this.productsList.slice(startIndex, endIndex);
}
onResize(event) { //to adjust to screen size
this.breakpoint = (event.target.innerWidth <= 800) ? 1 : 3;
}
希望有帮助.
这篇关于如何在垫板卡上使用角材料分页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!