question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Mat Paginator is not working properly along when used conditional rendering (*ngIf) on the outer div.

See original GitHub issue

Bug, feature request, or proposal:

Bug

What is the expected behavior?

When conditional rendering is done using *ngIf on table on outer <div>, Mat-Paginator should paginate properly

What is the current behavior?

If table is inside a conditional *ngIf and is hidden to start with, then after the condition is satisfied and view is initialized, data is not paginated. And the paginator is displayed as below screen shot 2018-02-28 at 7 43 58 pm

What are the steps to reproduce?

With conditional rendering where we can see the problem clearly https://stackblitz.com/edit/angular-material2-issue-s1hkz9?file=app/app.component.html

Without conditional rendering, it works properly https://stackblitz.com/edit/angular-material2-issue-3xb5aa?file=app/app.component.html

What is the use-case or motivation for changing an existing behavior?

We need to display a table with pagination only if certain condition is met

Which versions of Angular, Material, OS, TypeScript, browsers are affected?

"@angular/animations": "^5.2.0",
"@angular/cdk": "^5.2.0",
"@angular/common": "^5.2.0",
"@angular/compiler": "^5.2.0",
"@angular/core": "^5.2.0",
"@angular/forms": "^5.2.0",
"@angular/http": "^5.2.0",
"@angular/material": "^5.2.0",
"@angular/platform-browser": "^5.2.0",
"@angular/platform-browser-dynamic": "^5.2.0",
"@angular/router": "^5.2.0",
"typescript": "~2.5.3"

Is there anything else we should know?

NA

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:36
  • Comments:22 (1 by maintainers)

github_iconTop GitHub Comments

101reactions
Abhijith-Nagarajacommented, Mar 1, 2018

I have the solution. But I don’t know whether this is the correct way or a workaround. Please feel free to close this issue, if this is how it is designed

This is also applicable to MatSort.

Remove the following piece of code from ngAfterViewInit

@ViewChild(MatPaginator) paginator: MatPaginator;
@ViewChild(MatSort) sort: MatSort;
  ngAfterViewInit() {
    this.dataSource.paginator = this.paginator;
    this.dataSource.sort = this.sort;
  }

Add the following piece of code

  private paginator: MatPaginator;
  private sort: MatSort;

  @ViewChild(MatSort) set matSort(ms: MatSort) {
    this.sort = ms;
    this.setDataSourceAttributes();
  }

  @ViewChild(MatPaginator) set matPaginator(mp: MatPaginator) {
    this.paginator = mp;
    this.setDataSourceAttributes();
  }

  setDataSourceAttributes() {
    this.dataSource.paginator = this.paginator;
    this.dataSource.sort = this.sort;

    if (this.paginator && this.sort) {
      this.applyFilter('');
    }
  }
69reactions
lpalboucommented, Jun 19, 2018

Yes the *ngIf breaks the Material Paginator, however, instead of using *ngIf, you could use the [hidden] attribute like this:

<div [hidden]="isLoading">
  <mat-table [dataSource]="dataSource">
  ...
  </mat-table>
</div>

I have tested it on Angular 5 and it does work

Read more comments on GitHub >

github_iconTop Results From Across the Web

mat-paginator breaks when mat-table is inside of NgIf
If I set the default to the table view then pagination works fine unless I swap to grid view and back, if the...
Read more >
Mat Paginator Not Working Inside ngIf | CodeHandbook
So, the solution is to trigger the change detection using ChangeDetectorRef . Import it to app.component.ts and create an instance of it in ......
Read more >
Mat Paginator is not working properly along when ... - Freelancer
Mat Paginator is not working properly along when used conditional rendering (*ngIf) on the outer div. ... now building website with angular for...
Read more >
Mat Paginator is not working properly along when used conditional ...
Mat Paginator is not working properly along when used conditional rendering (*ngIf) on the outer div. Hello,freelancers. now building website with angular for ......
Read more >
mat-paginator not working with ngif, mat-paginator hidden, mat ...
Ask questions Mat Paginator is not working properly along when used conditional rendering (*ngIf) on the outer div. In the typescript file, we...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found