all
I tried to implement server side paging using angular material. Version listed below
"@angular/cli": "~8.0.6",
"@angular/compiler-cli": "~8.0.3",
"@angular/language-service": "~8.0.3"
"@angular/material": "^8.2.3"
The problem is total data count 29. Paginator show 1-10 of 29 when first loaded and page size set to 10. It is normal at this time.
If I click next page button, it shows 1-10 of 10 and next, last page button disabled. It should be 11-20 of 29. I dont know what is wrong here.
Here is the code snip:
--- html ---
<mat-paginator style="width: 100%" [length]="length" [pageIndex]="pageIndex" [pageSize]="pageSize"
[pageSizeOptions]="pageSizeOptions" showFirstLastButtons (page)="pageChanged($event)"></mat-paginator>
--- code ---
export class myComponent implements OnInit,AfterViewInit {
@ViewChild(MatPaginator, {static: false})
paginator: MatPaginator;
public displayedColumns: string[] = ['code', 'name', 'id',
'close', 'avg5p', 'avg20p', 'avg60p',
'avg5q', 'avg20q', 'avg60q',
'actions'];
public dataSource = new MatTableDataSource();
public type: string;
public last_updated: string;
length = 0;
pageIndex = 0;
pageSize = 10;
pageSizeOptions = [ 10, 20 ];
constructor(private sessionService: SessionService,private http: HttpClient,private dialog: MatDialog, private snackBar: MatSnackBar) {
this.type = "1";
this.dataSource.data = [];
this.dataSource.filteredData = [];
}
ngOnInit() {
}
ngAfterViewInit() {
this.dataSource.paginator = this.paginator;
this.getData();
}
getData() {
let query = {};
if (this.sessionService.token) {
query['token'] = this.sessionService.token;
}
query['type'] = this.type;
query['page_no'] = this.pageIndex;
query['page_rows'] = this.pageSize;
this.http.post(environment.api_url + "/api/myquery", query).subscribe((results:any) => {
if ( results.api_code == 0 ) {
this.last_updated = results.updated
this.dataSource.data = results.data;
this.length = results.totalRecords;
} else {
this.snackBar.open(this.sessionService.getMessage("SESSION_INVALID"), "", {
duration: 3000
});
this.sessionService.logout();
}
});
}
pageChanged(event: PageEvent) {
this.pageIndex = event.pageIndex;
this.pageSize = event.pageSize;
this.getData();
}
}
*** Please check comment from @Eliseo for solution. Thank you Eliseo.