title: How to get rowIndex in an Angular Material mat-cell
body: <p>I have a material table that I'm trying to set a selected class on the mat-cell definition, but I need to know the row index that the mat-cell is in. Below I'm trying the row index from the matRowDef in the call to isCellSelected when trying to set the class for the mat-cell, but it's undefined. Thanks in advance.</p>
  <pre><code>&lt;table mat-table [dataSource]=&quot;ds&quot; matSort style=&quot;width: 100%;&quot;&gt;
  
     
  
      &lt;ng-container *ngFor=&quot;let column of gridColumns&quot; [matColumnDef]=&quot;column&quot;&gt;
  
          &lt;th mat-header-cell *matHeaderCellDef mat-sort-header&gt;{{ column }}&lt;/th&gt;
  
          &lt;td mat-cell *matCellDef=&quot;let object; let cellIndex = index;&quot;
  
              (mousedown)=&quot;onCellMouseDown($event, cellIndex)&quot;
  
              (mouseup)=&quot;onCellMouseUp($event, cellIndex)&quot;
  
              [class.selected]=&quot;isCellSelected(cellIndex, rowIndex)&quot;&gt;{{ object[column] }}&lt;/td&gt;
  
      &lt;/ng-container&gt;
  
  
  
      &lt;tr mat-header-row *matHeaderRowDef=&quot;displayColumns&quot;&gt;&lt;/tr&gt;
  
      &lt;tr mat-row *matRowDef=&quot;let row; columns: displayColumns; let rowIndex = index;&quot;
  
          (click)=&quot;onRowClicked(row)&quot;
  
          (mousedown)=&quot;onRowMouseDown($event, rowIndex)&quot;
  
          (mouseup)=&quot;onRowMouseUp($event, rowIndex)&quot;&gt;&lt;/tr&gt;
  
  
  
  &lt;/table&gt;
  </code></pre>