[英]how retrieve value of selected row in angular material table
I am a beginner,I have a mat-table
:我是初学者,我有一张
mat-table
:
<table mat-table [dataSource]="list_equipment" matSort style="width: 100%;">
<!-- id Column -->
<ng-container matColumnDef="id">
<th mat-header-cell *matHeaderCellDef style="text-align: center !important;" mat-sort-header> id </th>
<td mat-cell *matCellDef="let list_equipment" style="text-align: center;"> {{list_equipment.id}} </td>
</ng-container>
<!-- CaptionCode Column -->
<ng-container matColumnDef="Caption">
<th mat-header-cell *matHeaderCellDef style="text-align: center !important;" mat-sort-header> caption</th>
<td mat-cell *matCellDef="let list_equipment" style="text-align: center;"> {{list_equipment.Caption}} </td>
</ng-container>
...
<tr mat-header-row *matHeaderRowDef="displayedColumnsequipment"></tr>
<!-- <tr mat-row *matRowDef="let row; columns: displayedColumnsequipment;" (click)="highlight(row)" [ngClass]="{'highlightTableColor': selectedRowIndex == row.position}"></tr> -->
<tr mat-row class="table-row" tabindex="1" *matRowDef="let row; columns: displayedColumnsequipment;" (click)="highlight(row)"></tr>
</table>
<mat-paginator [pageSizeOptions]="[10, 20, 50]" showFirstLastButtons></mat-paginator>
In TypeScript I write below code:在 TypeScript 我写下面的代码:
highlight(row){
this.selectedRow=row;
console.log(this.selectedRow);
}
The problem is that wherever I use the {{selectedRow.id}}
in html code, it gives this error in console: Cannot read property 'id' of undefined
问题是,无论我在 html 代码中使用
{{selectedRow.id}}
的任何地方,它都会在控制台中出现此错误: Cannot read property 'id' of undefined
you are defining the value which is not initialized so you have to set the value as "Safe Navigation Operator" "?"您正在定义未初始化的值,因此您必须将值设置为“安全导航运算符”“?”
{{selectedRow.id}} to {{selectedRow?.id}}
so this condition will not break your code and will not give you undefined or null value.所以这个条件不会破坏你的代码,也不会给你 undefined 或 null 值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.