我正在使用来自 Angular Material 的 MatTable
组件制作动态数据表。
我需要获取一行的当前位置。我可以很容易地获得用户点击的行,但我无法知道它在列表中的当前位置(这取决于排序/过滤/分页)。
有什么想法吗?
最佳答案
在你的 mat-cell
中你可以得到像 *ngFor
这样的索引,如下所示
<mat-cell *matCellDef="let element;let i = index;">
{{ i }}
</mat-cell>
更新 来自 Angular 5 也使用 index as i
<ng-container matColumnDef="rowIndex">
<th mat-header-cell *matHeaderCellDef> Index </th>
<td mat-cell *matCellDef="let element;index as i;"> {{ i }} </td>
</ng-container>
index
: number:iterable 中当前项的索引。first
: bool 值:当该项目是 iterable 中的第一个项目时为真。last
: bool 值:当该项目是可迭代对象中的最后一个项目时为真。even
: bool 值:当项目在可迭代对象中具有偶数索引时为真。odd
: bool 值:当项目在 iterable 中具有奇数索引时为真。
关于Angular Material - 获取数据表中行的索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50143099/