Is there an index property with CDK datasheet or Material2 datasheet?

I am using Angular2 Material Design Data Sheet in my application and it is awesome. I was wondering if there is a way to get the index number or line number? Something like row.index

? I noticed in the CDK datasheet documentation which mentions that "The directive also exports the same properties as ngFor (index, even, odd, first, last)" but has no examples on how to get the index.

Any help or guidance is greatly appreciated. Thank!

+3


source to share


2 answers


You can get the index of a row in the same way as *ngFor

add let i = index

in<md-row>

<md-row *cdkRowDef="let row; columns: displayedColumns; let i = index; let isOdd = odd; let isEven = even; let isLast = last" 
         [ngClass]="{'highlight': selectedRowIndex == row.id}"
         (click)="highlight(row, i, isOdd, isEven, isLast)">
</md-row>

      

c



highlight(row, index, oddFlag, evenFlag, lastFlag){
    alert("index:" + index + " odd: " + oddFlag + " even: " + evenFlag + " last: " + lastFlag);
    this.selectedRowIndex = row.id;
}

      

Plunker demo

+13


source


Thanks @Nehal for working for this example. I was trying to make a unique ID for each record using an index, so I also found a similar solution where I defined the index from cdkCellDef

. Below is my solution where is the index i

.



<md-cell *cdkCellDef="let row; let i = index;">
  <div id="{{i}}-info">
    index: {{i}}
    info: {{row.info}}
  </div>
</md-cell>

      

+6


source







All Articles