简体   繁体   English

创建可重复使用的 Angular 具有分页、过滤和排序功能的材料表

[英]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)可以采用pageNumberpageSize两个参数来提供所需的数据。

[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.

相关问题 角物料台分页,分类和过滤 - angular material table pagination, sorting and filtering 棱角分明的数据表,带有排序,分页和过滤页面问题 - angular material Data table with sorting, pagination, and filtering page issue 角度材料数据表,使用Firestore进行排序,分页和过滤 - Angular Material Data Table with sorting, pagination, and filtering using Firestore Angular 带有排序、分页和过滤的数据表 - Angular Data table with sorting, pagination, and filtering Angular 物料表排序和分页不起作用 - Angular Material table sorting and pagination not working Angular 带有分页过滤器和排序的材料表不起作用 - Angular Material table with pagination filter and sorting not working Angular 6:如何在角度材料表上实现分页和排序 - Angular 6: How to implement Pagination and Sorting on Angular Material Table 角垫表,排序,过滤和分页不会改变表的外观 - Angular mat-table, sorting, filtering, and pagination not changing the table appearance Angular Material 表没有排序、分页和过滤(使用 Angular、Nodejs 和 Mysql) - Angular Material table is not Sorting, Paginating and Filtering(using Angular, Nodejs & Mysql) angular 物料分页和排序问题 - angular material pagination and sorting issue
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM