【发布时间】:2020-05-29 20:10:31
【问题描述】:
我想与 mat-table 一起使用的数据不是平面的 - 有不同对象的层。我可以很好地显示数据,但排序不起作用。这是一个使用元素数据的示例,其中字段位于“foo”对象内的一层。我怎样才能使排序工作?
https://stackblitz.com/edit/angular-b8u3zr?file=src%2Fapp%2Ftable-sorting-example.ts
const ELEMENT_DATA: PeriodicElement[] = [
{foo: {position: 1, name: 'Hydrogen', weight: 1.0079, symbol: 'H'}},
{foo: {position: 2, name: 'Helium', weight: 4.0026, symbol: 'He'}},
{foo: {position: 3, name: 'Lithium', weight: 6.941, symbol: 'Li'}},
...
<table mat-table [dataSource]="dataSource" matSort class="mat-elevation-z8">
<ng-container matColumnDef="foo.position">
<th mat-header-cell *matHeaderCellDef mat-sort-header> No. </th>
<td mat-cell *matCellDef="let element"> {{element.foo.position}} </td>
</ng-container>
...
</table>
【问题讨论】: