2

I have a simple table like this

<table cdk-table [dataSource]="doors" [multiTemplateDataRows]="true" class="table table-hover">
  <ng-container cdkColumnDef="width">
    <th cdk-header-cell *cdkHeaderCellDef>Width</th>
    <td cdk-cell *cdkCellDef="let row; let idx = index;">
      Index: [{{idx}}]
      <span *ngIf="idx === undefined">undefined</span>
      <span *ngIf="idx === null">null</span>
    </td>
  </ng-container>

  <tr cdk-header-row *cdkHeaderRowDef="['width']"></tr>
  <tr cdk-row *cdkRowDef="let row; let i = index; columns: ['width']"></tr>
</table>

I need to access row index but it is undefined. What am I doing wrong?

Stackblitz: https://stackblitz.com/edit/angular-3cknv1

Liam
  • 27,717
  • 28
  • 128
  • 190
Vladimir Prudnikov
  • 6,974
  • 4
  • 48
  • 57

2 Answers2

2

You should use let idx = dataIndex;

Since you are using [multiTemplateDataRows]=true.

From the source:

/**
 * Context provided to the row cells when `multiTemplateDataRows` is true. This context is the same
 * as CdkCellOutletRowContext except that the single `index` value is replaced by `dataIndex` and
 * `renderIndex`.
 */

Look here for more info

Korfoo
  • 571
  • 2
  • 12
1

Its the same question as this SO question

you should use dataIndex like this:

<tr cdk-row *cdkRowDef="let row; let i = dataIndex;; columns: ['width']"></tr>
GaryB
  • 374
  • 1
  • 9