Angular dynamic data sorting table

Viewed 27

Im having a hard time on how to do a sorting with dynamic data in my table, the [dataSource]="dataSource" is my DTO or Model wherein I get the field that I needed. I dont know how to implement the sorting in my code. I can't do on my ts :dataSource = new MatTableDataSource because I already used it as my Model. Help please I dont know this one 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;
1 Answers

Try a setter along with MatTableDataSource

dataSource: MatTableDataSource<ChartofAccountsDTO> = new MatTableDataSource();

@ViewChild(MatSort)
set matSort(sort: MatSort) {
  this.dataSource.sort = sort;
}
Related