Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How To Change itemsPerPageLabel in mat-paginator in Angular 6+

I am using Angular 6.0.3 with Angular Material 7.1.0, I have my paginator in a separate component (that is not the app.component). What I have tried so far:

Created separate ts file called myPagniator.ts:

import {MatPaginatorIntl} from '@angular/material';

export class MyPaginatorLabel extends MatPaginatorIntl {

  itemsPerPageLabel = 'custome_label_name'; // customize item per page label

  constructor() {
    super();
  }
}

In my app.module.ts : I imported both MatPaginatorModule, MatPaginatorIntl from Angular Material. Inside providers array of app.module.ts, I put in MyPaginatorLabel and MatPaginatorIntl.

In the component which is using Angular MatPaginator, I extends MyPaginatorLabel class and have its constructor calls super() method, still it displays default text "itemsPerPage" after all this

What have I done wrong here ?? Can someone give me a bit of hint ?

like image 860
Edward Sun Avatar asked Jan 05 '19 22:01

Edward Sun


People also ask

How do you change Itemsperpagelabel in Mat Paginator?

Create subclass of MatPaginatorIntl in which you inject TranslateService. In the constructor translate the paginator labels and listen for language changes to trigger the same. The rangeLabel is translated with interpolation of the startIndex, endIndex and length variables. Save this answer.

How do I hide mat Paginator without data?

How to hide Pagination in Angular Material Datatable if there is no row? For this, we will simply add the [hidden] directive of Angular to show/ hide the element based on the boolean value provided. The [hidden] simply adds hidden attribute with style set to display:none if you are using bootstrap. css.

What is Paginator angular?

The paginator displays a dropdown of page sizes for the user to choose from. The options for this dropdown can be set via pageSizeOptions. The current pageSize will always appear in the dropdown, even if it is not included in pageSizeOptions.


1 Answers

Create a new TypeScript file and add a function that is exported and returns a MatPaginatorIntl object.

To modify the labels and text displayed, create a new instance of MatPaginatorIntl and include it in a custom provider - Angular Material - Paginator > API

CustomPaginatorConfiguration.ts

import { MatPaginatorIntl } from '@angular/material';

export function CustomPaginator() {
  const customPaginatorIntl = new MatPaginatorIntl();

  customPaginatorIntl.itemsPerPageLabel = 'Custom_Label:';

  return customPaginatorIntl;
}

Then add it to app.module.ts:

import { MatPaginatorIntl } from '@angular/material';
import { CustomPaginator } from './app/CustomPaginatorConfiguration';

@NgModule({
  // ...
  providers: [
    { provide: MatPaginatorIntl, useValue: CustomPaginator() }
  ]
})

You can also set the setting for a particular component like:

import { CustomPaginator } from './CustomPaginator';
import { MatPaginatorIntl } from '@angular/material';
/**
 * @title Paginator
 */
@Component({
  selector: 'your_component',
  templateUrl: 'your_component.html',
  providers: [
    { provide: MatPaginatorIntl, useValue: CustomPaginator() }  // Here
  ]
})

StackBlitz

like image 103
Prashant Pimpale Avatar answered Oct 17 '22 05:10

Prashant Pimpale