I have implemented angular material datatable which contains pagination and sorting but after fetching records from the database it's not working as expected.
report.componenet.html:
<table mat-table matSort [dataSource]="dataSource">
<!-- Position Column -->
<ng-container matColumnDef="date">
<th mat-header-cell *matHeaderCellDef mat-sort-header class="hide-arrow"> Date </th>
<td mat-cell *matCellDef="let element"> {{element.date}} </td>
</ng-container>
<!-- Name Column -->
<ng-container matColumnDef="name">
<th mat-header-cell *matHeaderCellDef mat-sort-header> Name </th>
<td mat-cell *matCellDef="let element"> {{element.name}} </td>
</ng-container>
<!-- Weight Column -->
<ng-container matColumnDef="download">
<th mat-header-cell *matHeaderCellDef mat-sort-header> Download </th>
<td mat-cell *matCellDef="let element">
<a [href]="element['download']['log']" target="_blank" *ngIf="element['download']['log'] !== ''" download>Log</a> <div *ngIf="element['download']['dataReport'] !== ''">|</div>
<a [href]="element['download']['dataReport']" target="_blank" *ngIf="element['download']['dataReport'] !== ''">Data report</a> <div *ngIf="element['download']['htmlReport'] !== ''">|</div>
<a [href]="element['download']['htmlReport']" target="_blank" *ngIf="element['download']['htmlReport'] !== ''">HTML report</a>
</td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
</table>
<mat-paginator [pageSizeOptions]="[5, 10, 20]"
showFirstLastButtons
aria-label="Select page of periodic elements">
</mat-paginator>
report.component.ts:
displayedColumns: string[] = ['date', 'name', 'download'];
dataSource = new MatTableDataSource<PeriodicElement>([]);
@ViewChild(MatPaginator) paginator: MatPaginator;
@ViewChild(MatSort) sort: MatSort;
ngAfterViewInit() {
this.dataSource.paginator = this.paginator;
this.dataSource.sort = this.sort;
}
getTestReportData() {
this.loginService.startSpinner(true);
this.service.fetchTestReportData().subscribe((resp: any) => {
console.log('resp python', resp);
this.loginService.startSpinner(false);
if(resp.status && resp.status === 'success') {
if(resp.body && resp.body.status && resp.body.status === 'success') {
if(resp.body.data && resp.body.data.length > 0) {
this.dataSource.data = resp.body.data;
this.length = resp.body.data.length;
console.log('datasource', this.dataSource);
}
}else{
this.presentAlert(resp.msg, 'danger', ALERT_TIMEOUT);
}
}else{
this.presentAlert(resp.msg, 'danger', ALERT_TIMEOUT);
}
})
}
I am fetching records from the server and appending them into the table but in this case, the pagination and sorting both are not getting reflected. check the sample output below.
There are two records present but it's showing 0 of 0 and also sorting is not working as expected. I need the pagination number and sorting should work as expected. How can I resolve this?
