【发布时间】:2021-12-19 12:00:28
【问题描述】:
我正在使用 Angular Material,并在模板的 for 循环中生成表格。我想为这些生成的表添加排序和分页,但我尝试的方法不起作用。
我有一个组列表,每个组都有它的项目,这是一个表格,我希望能够按 price, quantity, sales 排序。
groups: any[] = [
{
name: 'Group 1',
items: [
{ name: 'Product 1', price: 20, quantity: 33, sales: 11 },
{ name: 'Product 2', price: 10, quantity: 43, sales: 11 },
{ name: 'Product 3', price: 23, quantity: 63, sales: 11 },
],
},
{
name: 'Group 2',
items: [
{ name: 'Product 2', price: 40, quantity: 33, sales: 11 },
{ name: 'Product 4', price: 10, quantity: 143, sales: 211 },
{ name: 'Product 61', price: 63, quantity: 63, sales: 151 },
],
},
{
name: 'Group 20',
items: [
{ name: 'Product 22', price: 40, quantity: 33, sales: 11 },
{ name: 'Product 14', price: 10, quantity: 143, sales: 211 },
],
},
];
在 HTML 上我有这些:
<div *ngFor="let group of groups">
<p>{{ group.name }}</p>
<div class="mat-elevation-z8">
<table [dataSource]="group.items" mat-table matSort>
<ng-container matColumnDef="name">
<th *matHeaderCellDef mat-header-cell>Name</th>
<td *matCellDef="let element" mat-cell>{{ element.name }}</td>
</ng-container>
<ng-container matColumnDef="price">
<th *matHeaderCellDef mat-sort-header mat-header-cell>Price</th>
<td *matCellDef="let element" mat-cell>{{ element.price }}</td>
</ng-container>
<ng-container matColumnDef="quantity">
<th *matHeaderCellDef mat-sort-header mat-header-cell>Quantity</th>
<td *matCellDef="let element" mat-cell>{{ element.quantity }}</td>
</ng-container>
<ng-container matColumnDef="sales">
<th *matHeaderCellDef mat-sort-header mat-header-cell>Sales</th>
<td *matCellDef="let element" mat-cell>{{ element.sales }}</td>
</ng-container>
<tr *matHeaderRowDef="displayedColumns" mat-header-row></tr>
<tr *matRowDef="let row; columns: displayedColumns" mat-row></tr>
</table>
</div>
</div>
有人可以帮我吗?
【问题讨论】:
标签: javascript angular typescript sorting angular-material