components: Mat Paginator is not working properly along when used conditional rendering (*ngIf) on the outer div.
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
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
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 36
- Comments: 22 (1 by maintainers)
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
Add the following piece of code
Yes the *ngIf breaks the Material Paginator, however, instead of using *ngIf, you could use the [hidden] attribute like this:
I have tested it on Angular 5 and it does work
@rain01:
applyFilter()
method is a custom method. This is typically used to filter the datagrid/table.Here is the sample for that
This will still work if you remove the following
@ViewChild(MatPaginator, {static: true}) set matPaginator(paginator: MatPaginator) { this.dataSource.paginator = paginator; }
A quick and easy way to fix the issue, idk why this answer is not among the most upvoted.
I just wanted to make the paginator optional in my Table component, so I went for the SET solution.
Seems to me the issue happens when Angular initialises the datasource before the template has been rendered. So when the Mat-pagination is missing, it doesn’t seem to initialise the datasource properly. The
Set Fix in the ViewChild
as proposed by people above works for now.I had the same issue, and using “[hidden]” attribute as @lpalbou said, solved my problem. thanks @lpalbou .
notice the
new MatTableDataSource([]);
, that was my solution instead of*ngIf="dataSource"
I have the same issue but I’m trying to load the data from api. @Abhijith-Nagaraja What is
applyFilter()
? It says it doesn’t exist in my code.