获取对象数组索引号,并将其与对象的其他数据一起添加到mat-table中

时间:2020-02-02 17:02:17

标签: angular mat-table

我正在尝试获取每个元素的索引号,将其添加到具有数据的特定行中。我已经使用过*matCellDef="let element; let i = index",但是在mat-paginator上效果不佳。

预期结果:

enter image description here

component.ts

getNotifications(){     
    this.notificationService.getNotifications()
            .pipe(first())
            .subscribe(
                data => {
                  this.notifications = <Notification[]>data;
                  this.dataSource = new MatTableDataSource();
                  this.dataSource.data = data;
                  this.dataSource.paginator = this.paginator;
                },
                error => {
                  console.log("An Error Occurred");
                });
  }

1 个答案:

答案 0 :(得分:0)

索引仅适用于页面上显示的当前项目。您需要使用 pagesize pageindex 创建一个公式,例如{{(i)+(pageEvent.pageSize*pageEvent.pageIndex)}}

您将使用** pageEvent **从分页中获取大小和索引值

<mat-paginator #paginator
                 [pageSize]="10"
                 [pageSizeOptions]="[5, 10, 20]"
                 [showFirstLastButtons]="true"
                 (page)="pageEvent = $event">

click here for demo