Ser*_*i R 7 pagination typescript angular-material2
我想使用mat-card指令来展示我的产品.角度材料文档在我看来并不彻底.我在Internet上找到了许多使用Table with dataSource的示例(示例1,示例2)
现在我获得所有产品的productList并使用ngFor进行迭代.我在页面上显示所有产品.如何将productList提供给paginator并使用已处理的数据(paginationList)进行迭代.
*component.html文件显示所有产品:
<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>
Run Code Online (Sandbox Code Playgroud)
*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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
Sac*_*har 10
我有完全相同的要求,我使用mat-paginator 和包含 mat-cards 的 mat-grid-list做到了这一点。我使用 mat-grid-list 使我的列表具有响应性,以便它可以调整编号。根据屏幕尺寸排成一行的元素。这是我所做的:
<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>
Run Code Online (Sandbox Code Playgroud)
这是组件的样子:
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;
}
Run Code Online (Sandbox Code Playgroud)
希望能帮助到你。
这篇文章回答了这个问题。
observableData: Observable<any>;
ngOnInit() {
this.observableData = this.dataSource.connect();
}
Run Code Online (Sandbox Code Playgroud)
并在您的 HTML 文件中。
<mat-card *ngFor="let item of observableData | async">
</mat-card>
Run Code Online (Sandbox Code Playgroud)
我能够让这段代码工作。如果上面的代码不足以解释,我可以在这里发布整个代码(代码位于不同的机器上,因此现在不粘贴)。另请参阅上面的代码是在这里手写的(不是从编辑器粘贴的),因此可能会遇到小故障。
希望能帮助到你。
归档时间: |
|
查看次数: |
5546 次 |
最近记录: |