英文:
Access row data in material table header Cell
问题
要访问列中当前行的数据,我们可以使用以下代码:
<mat-cell *matCellDef="let rowdata">{{rowdata.name}}</mat-cell>
但我需要访问标头行上的数据。我们可以这样做吗?
<mat-header-cell *matHeaderCellDef="let rowdata">{{rowdata}}</mat-header-cell>
英文:
To access the data of current row in a column, we can use following code:
<mat-cell *matCellDef="let rowdata">{{rowdata.name}}</mat-cell>
But I need to access data on header row. Can we do that ? Like this
<mat-header-cell *matHeaderCellDef="let rowdata" >{{rowdata}}</mat-header-cell>
答案1
得分: 4
这是不可能的,也没有意义。您有0...n
个单元格,所以rowdata
取每个单元格的每个元素的值。但您只有一个表头。它唯一能够给您的合理数据是一个包含所有rowdata
的数组。
但是,您可以直接访问您传递的底层dataSource
。
<table mat-table [dataSource]="dataSource" class="mat-elevation-z8">
<ng-container matColumnDef="someColName">
<th mat-header-cell *matHeaderCellDef> {{dataSource[0].name}} </th>
<td mat-cell *matCellDef="let rowdata"> {{rowdata.name}} </td>
</ng-container>
</table>
英文:
That is not possible - and would not make sense. You have 0...n
cells, so rowdata
takes the value of each element for each cell. But you only have one table header. The only sensible data it could give you there is an array containing all rowdata
.
You could, however, access the underlying dataSource
you are passing in directly.
<table mat-table [dataSource]="dataSource" class="mat-elevation-z8">
<ng-container matColumnDef="someColName">
<th mat-header-cell *matHeaderCellDef> {{dataSource[0].name}} </th>
<td mat-cell *matCellDef="let rowdata"> {{rowdata.name}} </td>
</ng-container
</table>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论