[英]Angular material sorting table not working
I'm using Angular Material and I generate the tables inside a for loop in the template.我正在使用 Angular Material,并在模板的 for 循环内生成表格。 I want to add sorting and pagination to these generated tables but what I tried is not working.我想为这些生成的表格添加排序和分页,但我尝试的方法不起作用。 I have a list of groups and each group has its items which is a table and I want to be able to sort by price, quantity, sales
.我有一个组列表,每个组都有它的项目,这是一个表格,我希望能够按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 },
],
},
];
And On HTML I have these:在 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>
Can someone help me please I'm stuck?有人可以帮我吗,我被卡住了?
Check in this stackblitz 签入这个stackblitz
To include sorting in the code for the Material table, you will need to do the following:要在 Material 表的代码中包含排序,您需要执行以下操作:
Import the material sort module:导入物料分类模块:
import { MatSortModule } from '@angular/material/sort';
In addition, the following types are required to be imported within each component that uses Material tables and column sorting:此外,每个使用材料表和列排序的组件中都需要导入以下类型:
import { MatSort } from '@angular/material/sort';
To be able to use table sorting within the HTML template, declare an instance(s) of MatSort
as a child within our component source as shown:为了能够在 HTML 模板中使用表格排序,请在我们的组件源中声明一个MatSort
实例作为子项,如下所示:
@ViewChild(MatSort) sort1: MatSort;
@ViewChild(MatSort) sort2: MatSort;
@ViewChild(MatSort) sort3: MatSort;
Declare data source as an array:将数据源声明为数组:
dataSource: any[] = [
new MatTableDataSource(this.groups[0].items),
new MatTableDataSource(this.groups[1].items),
new MatTableDataSource(this.groups[2].items),
];
After our component is initialized, the sort for our data source is initialized with the following excerpt:在我们的组件初始化后,我们的数据源的排序将使用以下摘录进行初始化:
ngAfterViewInit()
{
this.dataSource[0].sort = this.sort1;
this.dataSource[1].sort = this.sort2;
this.dataSource[2].sort = this.sort3;
}
Add an additional id property into each of the group structures:在每个组结构中添加一个额外的 id 属性:
eg:例如:
{
id: 0,
name: 'Group 1',
..
},
The data source attribute in the HTML table element can be declared as: HTML table 元素中的数据源属性可以声明为:
<table [dataSource]="dataSource[group.id]" mat-table matSort>
Once the above changes are applied the table columns will have basic ascending and descending sorting.应用上述更改后,表格列将具有基本的升序和降序排序。
For a more general case I would recommend moving your table HTML into a component which can be properly re-used within the above loop with the data source as a parameter and the sorting objects with their own instance.对于更一般的情况,我建议将您的表格 HTML 移动到一个组件中,该组件可以在上述循环中正确重用,数据源作为参数,排序对象具有自己的实例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.