Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

StackOverflow Point

StackOverflow Point Navigation

  • Web Stories
  • Badges
  • Tags
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Web Stories
  • Badges
  • Tags
Home/ Questions/Q 2037
Alex Hales
  • 0
Alex HalesTeacher
Asked: May 31, 20222022-05-31T12:14:59+00:00 2022-05-31T12:14:59+00:00

count – Angular (custom) mat-paginator with lazy-evaluated total length

  • 0

[ad_1]

We currently have a simple mat-paginator that looks like this:
enter image description here

For the texts we use a custom MatPaginatorIntl for which we overwrite the nextPageLabel, previousPageLabel, and getRangeLabel (with ofLabel) to use our own translatable labels based on the chosen language of the user.
So far so good.

Some of the collection panels can be very large, so we want to have the total length lazy-evaluated. What I mean by this: in the screenshot above, the 496 is calculated with a count to the database and given to the front-end initially when the page is loaded. We would like to replace this count with an anchor-link "meer" (Dutch for "more") instead, and only when the user clicks on it, we do a separated call to the back-end and database to get the total count to replace that link. From a user’s perspective, 9 out of 10 times you only want to know that there are more pages, but don’t really care about how many more.

Unfortunately, the MatPaginatorIntl and mat-paginator both seem to be very basic. Too basic to make a change like this.
Our current idea is to replace the entire mat-paginator with our own component, which looks and acts exactly the same, but is customizable to our needs. But before I do that, I figured I’d make a question here in case anyone else has a better alternative by using the existing mat-paginator.


Not too relevant, but here is our current custom matPaginatorIntl:

import { MatPaginatorIntl } from '@angular/material/paginator';
import { TranslatePipe } from '../translate.pipe';

export class MatPaginatorIntlCustom extends MatPaginatorIntl {
  nextPageLabel="Next page";
  previousPageLabel="Previous page";
  ofLabel="of";
  getRangeLabel = this.createRangeLabel();

  private createRangeLabel(): (page: number, pageSize: number, length: number) => string {
    return (page, pageSize, length) => {
      if (length === 0 || pageSize === 0) {
        return `0 ${this.ofLabel} ${length}`;
      }
      if (pageSize === length) {
        page = 0;
      }
      length = Math.max(length, 0);
      const startIndex = page * pageSize;
      const endIndex = startIndex < length ? Math.min(startIndex + pageSize, length) : startIndex + pageSize;
      return `${startIndex + 1} - ${endIndex} ${this.ofLabel} ${length}`;
    };
  }

  inject(translate: TranslatePipe): MatPaginatorIntlCustom {
    this.nextPageLabel = translate.transform('nextpage');
    this.previousPageLabel = translate.transform('prevpage');
    this.ofLabel = translate.transform('ofpage');
    return this;
  }
}

As well as a snippet of how it’s used within our PaginatorComponent:

@Component({
  selector: 's-paginator',
  templateUrl: './paginator.component.html',
  styleUrls: ['./paginator.component.scss'],
  providers: [
    TranslatePipe, {
      provide: MatPaginatorIntl,
      useFactory: (translate: TranslatePipe) => new MatPaginatorIntlCustom().inject(translate),
      deps: [TranslatePipe]
    }],
})
export class PaginatorComponent extends Base {
  @ViewChild(MatPaginator) paginator?: MatPaginator;
  ...
}

[ad_2]

  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report
Leave an answer

Leave an answer
Cancel reply

Browse

Sidebar

Ask A Question

Related Questions

  • xcode - Can you build dynamic libraries for iOS and ...

    • 0 Answers
  • bash - How to check if a process id (PID) ...

    • 4996 Answers
  • database - Oracle: Changing VARCHAR2 column to CLOB

    • 1084 Answers
  • What's the difference between HEAD, working tree and index, in ...

    • 1028 Answers
  • Amazon EC2 Free tier - how many instances can I ...

    • 0 Answers

Stats

  • Questions : 43k

Subscribe

Login

Forgot Password?

Footer

Follow

© 2022 Stackoverflow Point. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.