aw3123
aw3123

Reputation: 139

Unable to display data in angular material table

Using angular material design data table, I need to fetch data from an REST API and display the response in the table.

But having issue in displaying the data in the table.

Modal.ts

export interface ReleaseModal {
    results: [{
        id: number;
        title: string;
    }]
} 

Service.ts

  getReleaseNotes(): Observable<ReleaseModal[]> {
      return this.http.get<ReleaseModal[]>(this.url);
  } 

component.ts

export class ReleasesComponent implements OnInit, AfterViewInit{
  title = 'Release notes';
  displayedColumns = ['releasenotes'];
  dataSource;

  @ViewChild(MatPaginator) paginator: MatPaginator;
  @ViewChild(MatSort) sort: MatSort;

  constructor ( private releaseNotes: ReleaseService ) {}

  ngOnInit() {
    this.releaseNotes.getReleaseNotes()
    .subscribe(data => {
      this.dataSource = new MatTableDataSource(data);
      console.log(data);
    });
  }

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

HTML

  <mat-table [dataSource]="dataSource" matSort>

    <ng-container matColumnDef="releasenotes">
      <mat-header-cell *matHeaderCellDef mat-sort-header> Release Notes </mat-header-cell>
      <mat-cell *matCellDef="let row"> {{row.results.title}}% </mat-cell>
    </ng-container>

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

When I console log the data, I could see the results but not sure why it is not displaying in the table. Please help

Below is the sample response, which I get from the API call

{
"results": [
{
"id": "203901655",
"title": "Test Page",
},
....
}

Upvotes: 1

Views: 6791

Answers (2)

User3250
User3250

Reputation: 3421

Change ngOnInit as below, should fix it:

dispalyedColumns=['title']
ngOnInit() {
    this.releaseNotes.getReleaseNotes()
    .subscribe(data => {
      this.dataSource = new MatTableDataSource(data.results);
      this.dataSource.paginator = this.paginator;
      this.dataSource.sort = this.sort;
      console.log(data);
    });
}

Remove the lines in ngAfterViewInit. The issue is since ngAfterViewInit gets called before you get data from service and dataSource is still undefined.

HTML:

<ng-container matColumnDef="title">
  <mat-header-cell *matHeaderCellDef mat-sort-header> Release Notes </mat-header-cell>
  <mat-cell *matCellDef="let row"> {{row.title}}% </mat-cell>
</ng-container>

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

Upvotes: 2

SEY_91
SEY_91

Reputation: 1697

Try this one:

export class ReleasesComponent implements OnInit, AfterViewInit{
  title = 'Release notes';
  displayedColumns = ['releasenotes'];
  dataSource = new MatTableDataSource<ReleaseModal>();

  @ViewChild(MatPaginator) paginator: MatPaginator;
  @ViewChild(MatSort) sort: MatSort;

  constructor ( private releaseNotes: ReleaseService ) {}

  ngOnInit() {
    this.releaseNotes.getReleaseNotes()
    .subscribe(data => {
      this.dataSource.data = data;
      console.log(data);
    });
  }

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

Upvotes: 0

Related Questions