0

I followed stackblitz MatDataTable. The table is not populating until i click pagination links. After clicking pagination everything works normal.I am trying since last day. I tried this stackoverflow solution as well but didn't workde:

table.component.ts

import { Component, OnInit, ViewChild } from '@angular/core';
import { MatPaginator, MatTableDataSource } from '@angular/material';


@Component({
  selector: 'app-table',
  templateUrl: './table.component.html',
  styleUrls: ['./table.component.scss']
})
export class TableComponent implements OnInit {
  displayedColumns: string[] = ['position', 'name', 'weight', 'symbol'];
  dataSource = new MatTableDataSource<PeriodicElement>(ELEMENT_DATA);

  @ViewChild(MatPaginator) paginator: MatPaginator;      

  ngOnInit() {
    this.dataSource.paginator = this.paginator;
  }
}

export interface PeriodicElement {
  name: string;
  position: number;
  weight: number;
  symbol: string;
}

const ELEMENT_DATA: PeriodicElement[] = [
  {position: 1, name: 'Hydrogen', weight: 1.0079, symbol: 'H'},
  {position: 2, name: 'Helium', weight: 4.0026, symbol: 'He'},
  {position: 3, name: 'Lithium', weight: 6.941, symbol: 'Li'},
  {position: 4, name: 'Beryllium', weight: 9.0122, symbol: 'Be'},
  {position: 5, name: 'Boron', weight: 10.811, symbol: 'B'},
  {position: 6, name: 'Carbon', weight: 12.0107, symbol: 'C'},
  {position: 7, name: 'Nitrogen', weight: 14.0067, symbol: 'N'},
  {position: 8, name: 'Oxygen', weight: 15.9994, symbol: 'O'}
];

table.component.html

Copied same data from here. and removed the table, tr, th and td tags. as per google suggestion.

<div class="mat-elevation-z8">
  <mat-table [dataSource]="dataSource">

    <!-- Position Column -->
    <ng-container matColumnDef="position">
      <mat-header-cell *matHeaderCellDef> No. </mat-header-cell>
      <mat-cell *matCellDef="let element"> {{element.position}} </mat-cell>
    </ng-container>

    <!-- Name Column -->
    <ng-container matColumnDef="name">
      <mat-header-cell *matHeaderCellDef> Name </mat-header-cell>
      <mat-cell *matCellDef="let element"> {{element.name}} </mat-cell>
    </ng-container>

    <!-- Weight Column -->
    <ng-container matColumnDef="weight">
      <mat-header-cell *matHeaderCellDef> Weight </mat-header-cell>
      <mat-cell *matCellDef="let element"> {{element.weight}} </mat-cell>
    </ng-container>

    <!-- Symbol Column -->
    <ng-container matColumnDef="symbol">
      <mat-header-cell *matHeaderCellDef> Symbol </mat-header-cell>
      <mat-cell *matCellDef="let element"> {{element.symbol}} </mat-cell>
    </ng-container>

    <mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
    <mat-row *matRowDef="let row; columns: displayedColumns;"></mat-row>
  </mat-table>

  <mat-paginator [pageSizeOptions]="[5, 10, 20]" showFirstLastButtons></mat-paginator>
</div>

Package.json consists:

"@agm/core": "1.0.0-beta.2",
"@angular/animations": "5.2.9",
"@angular/cdk": "5.2.4",
"@angular/common": "5.2.9",
"@angular/compiler": "5.2.9",
"@angular/core": "5.2.9",
"@angular/flex-layout": "5.0.0-beta.13",
"@angular/forms": "5.2.9",
"@angular/http": "5.2.9",
"@angular/material": "5.2.4",
"@angular/cli": "1.7.3",
chandoo
  • 1,276
  • 2
  • 21
  • 32
  • 1
    Aniket has the answer for you. If you want are more involved example check my StackBliz https://stackblitz.com/edit/angular-material-table-with-multi-queries. Lots of treats in there to study. – Preston Aug 10 '18 at 04:58
  • TQ Peterson, I will appreciate if you wrote any tutorial on the above stackblitz example, explaining the rxjs operators to understand better. – chandoo Aug 10 '18 at 05:11
  • 1
    :-) That StackBliz is my tutorial but you have to study the Angular Material docs first. As you study then refer to my table. I did disagree with the docs a little but that is in the comments. I try to keep it updated. – Preston Aug 10 '18 at 21:22

1 Answers1

3

Initialise pagination on ngAfterViewInit hooks lifecycle

here's example,

ngAfterViewInit() {
    this.dataSource.paginator = this.paginator;
}

You are using paginator on onInit(), remove from onInit()

Aniket Avhad
  • 4,025
  • 2
  • 23
  • 29
  • Thank you Aniket, I tried using ChangeDetectorRef and made it work. Anyhow, your solution is fine. – chandoo Aug 10 '18 at 05:06