gpt4 book ai didi

typescript - 如何在垫卡上使用角度 Material 分页?

转载 作者:搜寻专家 更新时间:2023-10-30 20:40:46 25 4
gpt4 key购买 nike

我想使用ma​​t-card 指令来展示我的产品。角度 Material docs在我看来并不彻底。我在 Internet 上找到了很多将 Table 与 dataSource ( example 1 , example 2 )

结合使用的示例

现在我得到包含所有产品的 productList 并使用 ngFor 对其进行迭代。我在页面上显示所有产品。如何将 productList 提供给分页器并迭代处理后的数据 (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>

*组件.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();
}
}
}

最佳答案

我有完全相同的要求,我使用 ma​​t-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>

这是组件的样子:

  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;
}

希望对您有所帮助。

关于typescript - 如何在垫卡上使用角度 Material 分页?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50810413/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com