In my Angular 5 project, I am utilizing Angular Material table to create a grid. The data is fetched from an HTTP request and stored in a variable named dataSourceNew within the view.ts file. Since dataSourceNew has dynamic content and structure, no interface is exported.
this.http.get('http://example.org?,{headers: this.headers})
.subscribe(
res => {
this.displayedColumnsNew = res.record;
this.dataSourceNew = res.data;
this.matdatasource = new MatTableDataSource(this.dataSourceNew);
this.dataSourceNew.paginator = this.paginator;
console.log("got matdatasource object",this.matdatasource);
});
I have successfully created a data-table using the provided syntax in the HTML file.
<div class="example-container mat-elevation-z8">
<mat-table #table [dataSource]="dataSourceNew">
<ng-container *ngFor="let head of tableData.record; let i= index; " matColumnDef="{{head}}" (click)="setClickedRow(ddata) " [class.active]="ddata[primaryid] == selectedRow">
<mat-header-cell *matHeaderCellDef> {{tableData.gridhead[i]}}
</mat-header-cell>
<mat-cell *matCellDef="let element"> {{element[head]}} </mat-cell>
</ng-container>
<mat-header-row *matHeaderRowDef="displayedColumnsNew"></mat-header-row>
<mat-row *matRowDef="let row; columns: displayedColumnsNew;">
</mat-row>
</mat-table>
</div>
To add pagination to the table, I declared this:
@ViewChild(MatPaginator) paginator: MatPaginator;
However, when I tried attaching the pagination to the table in the HTML file as shown below:
<mat-paginator #paginator
[pageSize]="10"
[pageSizeOptions]="[5, 10, 20]"
[showFirstLastButtons]="true">
</mat-paginator>
An error occurred in the console that broke my application. Despite importing MatPaginator and MatTableDataSource in the respective .ts file:
Uncaught Error: Template parse errors:
Can't bind to 'pageSize' since it isn't a known property of 'mat-paginator'.
1. If 'mat-paginator' is an Angular component and it has 'pageSize' input, then verify that it is part of this module.
2. If 'mat-paginator' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message.
3. To allow any property add 'NO_ERRORS_SCHEMA' to the '@NgModule.schemas' of this component.
The datasource log revealed that the paginator element of this object is empty. How can I utilize the Angular Material paginator without creating an interface?
Update :
Following David's suggestion, I added MatPaginator to the imports of the main app module. Now, the pagination block displays perfectly.
However, the Current Records indicate "0 of 0," even though there are 14 records, and features such as changing the number of records, next, previous, etc., do not work. What could be missing?
Update 2: By adding the Length property to the paginator tags, the pagination functions properly. The issue now lies with the datatable not adjusting the current number of rows.
Update 3: Upon implementing Pierre Mallet's suggestion, total records are inferred from the API response without explicitly defining the [length]. The paginator is correctly bound with my
this.matdatasource = new MatTableDataSource([]);
. However, I wish to initially fetch a few records (let's say 20) and subsequently call the database for more requests, avoiding hitting the DB at once for all records. How can this be achieved?