[英]Creating reusable Angular Material table with pagination, filtering and sorting
I'm working with Angular Material in my project.我在我的项目中使用 Angular 材料。 I'll use many tables in my project and because of the high complexity of this project, I want to use a reusable table in order to avoid code duplication.
我将在我的项目中使用很多表,并且由于该项目的高度复杂性,我想使用可重用的表以避免代码重复。 I tried to create a reusable table without filtering and pagination:
我试图创建一个没有过滤和分页的可重用表:
table.component.html表.component.html
<table mat-table [dataSource]="dataSource" class="mat-elevation-z8">
<ng-container
*ngFor="let disCol of tableColumns let colIndex = index;"
matColumnDef="{{disCol.columnDef}}"
>
<th mat-header-cell *matHeaderCellDef>
{{ disCol.header }}
</th>
<td mat-cell *matCellDef="let element">
<span *ngIf="!disCol.isLink; else link">
{{ disCol.cell(element) }}
</span>
<ng-template #link>
<a [routerLink]="[disCol.url]">
{{ disCol.cell(element) }}
</a>
</ng-template>
</td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
</table>
table.component.ts表.component.ts
export class TableComponent implements OnInit {
@Input()
tableColumns: Array<TableColumn> = [];
@Input()
tableData: Array<any> = [];
displayedColumns: Array<string> = [];
dataSource: MatTableDataSource<any> = new MatTableDataSource();
constructor() {}
ngOnInit(): void {
this.displayedColumns = this.tableColumns.map((c) => c.columnDef);
this.dataSource = new MatTableDataSource(this.tableData);
}
}
TableColumn.ts表列.ts
export interface TableColumn {
columnDef: string;
header: string;
cell: Function;
isLink?: boolean;
url?: string;
}
I want to add server-side pagination, filtering and sorting.我想添加服务器端分页、过滤和排序。 My API (ASP.NET Core API based on EF Core) can take two parameters of
pageNumber
and pageSize
to provide desired data.我的 API(基于 EF Core 的 ASP.NET Core API)可以采用
pageNumber
和pageSize
两个参数来提供所需的数据。
[HttpGet]
public async Task<IActionResult> GetEquipmentFilterCategory(int pageNumber, int pageCapacity, string categoryName)
{
int skip = (pageNumber - 1) * pageCapacity;
var list = await _sqlServerContext.Equipments.Where(x => x.EquipmentCategory!.CategoryName == categoryName)
.OrderByDescending(x => x.EquipmentId)
.Skip(skip).Take(pageCapacity)
.Include(x => x.CostCenter)
.Include(x => x.EquipmentCategory)
.Include(x => x.equipmentType)
.ToListAsync();
if (list.Count > 0)
{
return Ok(list);
}
return NoContent();
}
[HttpGet]
public async Task<IActionResult> GetTotalEquipmentCount()
{
var totalCount = await _sqlServerContext.Equipments.CountAsync();
return Ok(totalCount);
}
How can I add server-side pagination, filtering and soring to this reusable MatTable?如何向这个可重用的 MatTable 添加服务器端分页、过滤和排序?
What you're looking for is already in Angular Material Table's examples as a Wrapper Table
( https://material.angular.io/components/table/examples#table-wrapped ).您正在寻找的内容已经在 Angular 材料表的示例中作为
Wrapper Table
( https://material.angular.io/components/table/examples#table-wrapped )。 You can take this as a starting point for implementing this the way you want.您可以以此为起点,以您想要的方式实现它。 All you need to do now is to add
ContentChild
for MatPaginator
and inject MatSort
in constructor.您现在需要做的就是为
MatPaginator
添加ContentChild
并在构造函数中注入MatSort
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.