角度动态数据排序表



我很难理解如何对表中的动态数据进行排序,[dataSource]="dataSource";是我的DTO或Model,在其中我可以获得所需的字段。我不知道如何在代码中实现排序。我不能对我的ts:dataSource=new MatTableDataSource执行操作,因为我已经将其用作我的模型。请帮忙,我不知道这个T_T

.html

<mat-table mat-table [dataSource]="dataSource" matSort>
<ng-container matColumnDef="classification">
<mat-header-cell *matHeaderCellDef mat-sort-header> Classification </mat-header-cell>
<mat-cell *matCellDef="let element"> {{element.groupClassificationDTO.classificationDTO.nameClassification}} </mat-cell>
</ng-container>  
<ng-container matColumnDef="subClassification">
<mat-header-cell *matHeaderCellDef mat-sort-header> Sub-Classification </mat-header-cell>
<mat-cell *matCellDef="let element"> {{element.groupClassificationDTO.subClassificationDTO.nameSubClassification}} </mat-cell>
</ng-container>                 
<mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
<mat-row *matRowDef="let row; columns: displayedColumns;"></mat-row>
</mat-table>

ts。

dataSource: ChartofAccountsDTO[] = [];
@ViewChild(MatSort, {static: true}) sort: MatSort;

尝试一个setter和MatTableDataSource

dataSource: MatTableDataSource<ChartofAccountsDTO> = new MatTableDataSource();
@ViewChild(MatSort)
set matSort(sort: MatSort) {
this.dataSource.sort = sort;
}

最新更新