Write a custom version of getRangeLabel
This is a simple, flexible, and powerful approach and allows you to customize the full paginator text instead of just one part of it.
In your component, set up your paginator:
@ViewChild(MatPaginator, {static : false}) paginator!: MatPaginator;
Then assign a custom function in the afterViewInit
:
ngAfterViewInit() {
this.paginator._intl.getRangeLabel = this.getRangeDisplayText;
}
Then set up a custom function to display the text you need:
getRangeDisplayText = (page: number, pageSize: number, length: number) => {
const initialText = `Displaying users`; // customize this line
if (length == 0 || pageSize == 0) {
return `${initialText} 0 of ${length}`;
}
length = Math.max(length, 0);
const startIndex = page * pageSize;
const endIndex = startIndex < length
? Math.min(startIndex + pageSize, length)
: startIndex + pageSize;
return `${initialText} ${startIndex + 1} to ${endIndex} of ${length}`; // customize this line
};
This will use whatever pageSize
and length
you have in your HTML or have configured in your component.
<mat-paginator [length]="dataSource.total"
[pageSize]="10"
hidePageSize
showFirstLastButtons>
If your data returns 50 recordes, this will show this text:
Displaying users 1 to 10 of 50