我可以获取角形材料表的标题单元格的索引吗?

问题描述

我可以用表中标头单元格的实际索引替换[0]吗?

类似[ngClass] =“ {'highlight':highlightedRows [this.elem.cellIndex] }”

这是代码

<ng-container matColumnDef="position">
    <th mat-header-cell 
      *matHeaderCellDef
      (click)="highlightColumn($event)"
      [ngClass]="{'highlight':highlightedRows[0]}"
    > No.</th>
    <td mat-cell *matCellDef="let element"
    [ngClass]="{'highlight':highlightedRows[0]}"

    > {{element.position}} </td>
  </ng-container>

谢谢

解决方法

更改为:

*matCellDef="let element; let i = index;"

然后,您可以使用i代替0。

,

我认为您需要重新设计表并使用类似的东西。使用* ngFor

创建列
<ng-container matColumnDef="{{column}}" *ngFor="let column of columnsToDisplay,let i = index">
     <th mat-header-cell *matHeaderCellDef> {{column}} {{i}}</th>
     <td mat-cell *matCellDef="let element"> {{element[column]}} </td>
</ng-container>

更多您在https://material.angular.io/components/table/examples的展开式表格中找到了