mat-paginator does not work in the ng-template

1.3k Views Asked by At

I have some mat-table with mat-paginator like this:

                 <table mat-table style="width: 100%;" [dataSource]="dataSource">
                    <ng-container matColumnDef="evento">
                        <th mat-header-cell *matHeaderCellDef> Evento </th>
                        <td mat-cell *matCellDef="let row"> {{row.evento}} </td>
                    </ng-container>
                    <ng-container matColumnDef="fecha">
                        <th mat-header-cell *matHeaderCellDef> Fecha </th>
                        <td mat-cell *matCellDef="let row">{{row.fecha}} </td>
                    </ng-container>
                    ..... more columns .....
                    <ng-container matColumnDef="edit">
                        <th mat-header-cell *matHeaderCellDef style="text-align: right; margin-right: 8px;"> Edit </th>
                        <td mat-cell *matCellDef="let row" style="text-align: right;">
                            <button mat-icon-button (click)="editEvent(row)">
                                <mat-icon>edit</mat-icon>
                            </button>
                        </td>
                    </ng-container>

                    <tr mat-header-row *matHeaderRowDef="displColumns"></tr>
                    <tr mat-row *matRowDef="let row; columns: displColumns"></tr>
                </table>
                <mat-paginator [pageSize]="5" [pageSizeOptions]="[5, 10, 20]" showFirstLastButtons></mat-paginator>

and in code behind

@ViewChild(MatPaginator, {static: true}) paginator: MatPaginator;

and in ngOnInit

this.dataSource.paginator = this.paginator;

every work fine before put the table inside de ng-template, i want use ng-template directive to change between list of events and add new event

<div *ngIf="EventSelect then listEv else addNew">
</div>

someone have idea what i´m lost.

1

There are 1 best solutions below

3
On BEST ANSWER

In case you wrap your html in ng-template you could use setter as follows:

@ViewChild(MatPaginator) set paginator(value: MatPaginator) {
  this.dataSource.paginator = value;
}

Note that I removed static: true option from @ViewChild decorator.

Stackblitz Example