我正在寻找一种在md-table中高亮显示整行的好方法。
我应该做指令还是什么?
有人可以帮我这个吗?
我应该做指令还是什么?
有人可以帮我这个吗?
<div class="example-container mat-elevation-z8"> <md-table #table [dataSource]="dataSource"> <!--- Note that these columns can be defined in any order. The actual rendered columns are set as a property on the row definition" --> <!-- ID Column --> <ng-container cdkColumnDef="userId"> <md-header-cell *cdkHeaderCellDef> ID </md-header-cell> <md-cell *cdkCellDef="let row"> {{row.id}} </md-cell> </ng-container> <!-- Progress Column --> <ng-container cdkColumnDef="progress"> <md-header-cell *cdkHeaderCellDef> Progress </md-header-cell> <md-cell *cdkCellDef="let row"> {{row.progress}}% </md-cell> </ng-container> <!-- Name Column --> <ng-container cdkColumnDef="userName"> <md-header-cell *cdkHeaderCellDef> Name </md-header-cell> <md-cell *cdkCellDef="let row"> {{row.name}} </md-cell> </ng-container> <!-- Color Column --> <ng-container cdkColumnDef="color"> <md-header-cell *cdkHeaderCellDef>Color</md-header-cell> <md-cell *cdkCellDef="let row" [style.color]="row.color"> {{row.color}} </md-cell> </ng-container> <md-header-row *cdkHeaderRowDef="displayedColumns"></md-header-row> <md-row *cdkRowDef="let row; columns: displayedColumns;"></md-row> </md-table> </div>
更新材料版本更新(md – > mat):
原文链接:https://www.f2er.com/angularjs/143865.htmlHTML:
<!-- Add the highlight class in row definiton of md-table --> <!-- Add click event to pass the selected row index --> <mat-row *cdkRowDef="let row; columns: displayedColumns;" [ngClass]="{'highlight': selectedRowIndex == row.id}" (click)="highlight(row)"> </mat-row>
原答案:
您可以使用ngClass和selectedRowIndex之类的标志来完成。每当点击的行索引等于selectedRowIndex时,将应用该类。
HTML:
<!-- Add the highlight class in row definiton of md-table --> <!-- Add click event to pass the selected row index --> <md-row *cdkRowDef="let row; columns: displayedColumns;" [ngClass]="{'highlight': selectedRowIndex == row.id}" (click)="highlight(row)"> </md-row>
CSS:
.highlight{ background: #42A948; /* green */ }
TS:
selectedRowIndex: number = -1; highlight(row){ this.selectedRowIndex = row.id; }