jij*_*ijo 3 angular-material angular
我有一个 angular 6 应用程序,它有一个 Mat Table。表的数据填充如下。排序适用于字段position
and name
,但不适用于weight
和symbol
which 是detail
对象的一部分。我也尝试将列命名为detail.weight
in displayedColumns
,matColumnDef
但没有运气。请告知我做错了什么?
const ELEMENT_DATA: PeriodicElement[] = [
{position: 1, name: 'Hydrogen', detail: {weight: 4.0026, symbol: 'He'}},
{position: 2, name: 'Helium', detail: {weight: 4.0026, symbol: 'He'}},
]
displayedColumns: string[] = ['position', 'name', 'weight', 'symbol'];
Run Code Online (Sandbox Code Playgroud)
模板
<ng-container matColumnDef="weight">
<th mat-header-cell *matHeaderCellDef mat-sort-header> Weight </th>
<td mat-cell *matCellDef="let element"> {{element.detail.weight}} </td>
</ng-container>
Run Code Online (Sandbox Code Playgroud)
这是该问题的stackblitz url
https://stackblitz.com/edit/angular-kumdeq
使用 sortingDataAccessor,它
允许通过覆盖 sortingDataAccessor 进行排序自定义,它定义了如何访问数据属性。
在 ngOnInit 中添加以下代码片段:
this.dataSource.sortingDataAccessor = (item, property) => {
switch (property) {
case 'weight': return item.detail.weight;
case 'symbol': return item.detail.symbol;
default: return item[property];
}
Run Code Online (Sandbox Code Playgroud)
链接:https : //stackblitz.com/edit/angular-kumdeq-z4bags
归档时间: |
|
查看次数: |
2041 次 |
最近记录: |