javascript - Angular Material:如何在 mat-table 中选择特定的 mat-cell?
问题描述
我有一个显示设备列表的垫子表。
它的代码是
<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>
现在,单击特定单元格时,我想为其添加样式,以表明它处于活动状态。但是,当我尝试使用 ngClass 执行此操作时,样式会添加到所有单元格中。
像这样:
请帮忙。
解决方案
您需要将您选择的值与整个表格数据进行比较
onDeviceSelect(element){
this.addClass = false;
this.tableData.forEach(element = > {
if(element.VIN === event.VIN) {
this.addClass = true;
}
});
}
在模板中
<mat-cell [ngClass]="{'mat-selected-style':addClass}" (click)="onDeviceSelect(element)" *matCellDef="let element">
<!-- {{element.model.instrument.VIN}} -->
{{element.VIN}}
</mat-cell>
推荐阅读
- kubernetes - 入口 Openshift
- google-bigquery - 如何向 BigQuery 外部表添加列
- mysql - 如何将 UTC 中的日期时间保存到以 UTC+1 运行的远程 MySQL 服务器?
- python - Manim,ReplacementTransform 没有变换
- javascript - 查询字符串到firestore Angular - 多种选择
- python - 在不滞后主进程的同时更新单独进程的线程
- oracle - 插入之前的Oracle触发器未插入
- r - 闪亮的仪表板 - 带有复选框值的反应条形图
- android - 滚动时多个 viewType RecyclerView imageView 大小变化
- r - 如何将参数传递给闪亮的应用程序?