[英]Angular Material : How to select a specific mat-cell in mat-table?
I have a mat table which displays list of Devices.我有一个显示设备列表的垫子表。
The code for it is它的代码是
<mat-table [dataSource] = "deviceDataList">
<ng-container matColumnDef="numbers">
<mat-header-cell *matHeaderCellDef>
<div class="search-container">
<mat-form-field class="search-form-field no-line" floatLabel="never">
<button mat-button matPrefix mat-icon-button aria-label="Search" (click)="applyFilter()">
<mat-icon>search</mat-icon>
</button>
<input matInput [(ngModel)] = "searchKey" placeholder="Search" autocomplete="off" (keyup)="applyFilter()">
<button mat-button matSuffix mat-icon-button aria-label="Clear" *ngIf="searchKey" (click)="onSearchClear()">
<mat-icon>close</mat-icon>
</button>
</mat-form-field>
</div>
</mat-header-cell>
<mat-cell [ngClass]="{'mat-style':true, 'mat-selected-style':btnValue}" (click)="onDeviceSelect(element)" *matCellDef="let element">
<!-- {{element.model.instrument.VIN}} -->
{{element.VIN}}
</mat-cell>
</ng-container>
<ng-container matColumnDef="loading">
<mat-footer-cell *matFooterCellDef colspan="6">
Loading Data...
</mat-footer-cell>
</ng-container>
<ng-container matColumnDef="noData">
<mat-footer-cell *matFooterCellDef colspan="6">
No Data.
</mat-footer-cell>
</ng-container>
<mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
<mat-row *matRowDef="let row; columns: displayedColumns;"></mat-row>
<mat-footer-row *matFooterRowDef="['loading']" [ngClass]="{'hide':deviceDataList!=null}"></mat-footer-row>
<mat-footer-row *matFooterRowDef="['noData']" [ngClass]="{'hide':!(deviceDataList!=null && deviceDataList.data.length==0)}"></mat-footer-row>
</mat-table>
Now, On clicking a particular cell I want to add a style to it, inorder to indicate that it is active.现在,单击特定单元格时,我想为其添加样式,以表明它处于活动状态。 But, when I try to do it using ngClass, The style gets added to all the cells.
但是,当我尝试使用 ngClass 执行此操作时,样式会添加到所有单元格中。
Like this:像这样:
Please help.请帮忙。
You need to compare your selected value with your whole table Data您需要将您选择的值与整个表格数据进行比较
onDeviceSelect(element){
this.addClass = false;
this.tableData.forEach(element = > {
if(element.VIN === event.VIN) {
this.addClass = true;
}
});
}
in template在模板中
<mat-cell [ngClass]="{'mat-selected-style':addClass}" (click)="onDeviceSelect(element)" *matCellDef="let element">
<!-- {{element.model.instrument.VIN}} -->
{{element.VIN}}
</mat-cell>
What you need, is to save the current clicked/selected cell and then bind the conditional styling on it.您需要的是保存当前单击/选定的单元格,然后在其上绑定条件样式。 Something like this:
像这样的东西:
public currentCell;
public onClick(cell): void{
this.currentCell = cell;
}
in template you would do:在模板中你会做:
<mat-cell [ngClass]="{'mat-selected-style':this.currentCell == cell}" (click)="onClick(cell)" *matCellDef="let cell">
<!-- {{element.model.instrument.VIN}} -->
{{element.VIN}}
</mat-cell>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.