Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mat-paginator breaks when mat-table is inside of NgIf

I have an angular project that is using mat-table and mat-paginator for a certain view, the problem is the view has a grid view and table view with a toggle, the grid view is default and table is hidden using an NgIf when the grid view is active. If I set the default to the table view then pagination works fine unless I swap to grid view and back, if the default if set to grid it breaks when I swap to the table view. I'm guessing its because the table is hidden when this code runs:

this.sliceList = new MatTableDataSource<Slice>(result);
this.sliceList.paginator = this.paginator;

I tried console logging this.sliceList and sliceList.paginator is undefined when the grid view is defaulted so I assuming this is the issue. How Can I fix this?

like image 893
jmona789 Avatar asked May 07 '19 18:05

jmona789


People also ask

What is MatColumnDef?

MatColumnDef extends CdkColumnDefDefines a set of cells available for a table column.

How do I change my mat Paginator label?

The labels for the paginator can be customized by providing your own instance of MatPaginatorIntl . This will allow you to change the following: The label for the length of each page. The range text displayed to the user.

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

1st Solution

Move mat-paginator from inside *ngIf div to outside

2nd Solution

use static false while declaring MatPaginator or MatSort

@ViewChild(MatPaginator, {static: false}) paginator: MatPaginator;
@ViewChild(MatSort, {static: false}) sort: MatSort;
like image 58
Muhammad Bilal Avatar answered Nov 10 '22 09:11

Muhammad Bilal