Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to bind to data in mat-table

Issue

I am trying to implement a mat-table in Angular 7 but it is giving me the following error:

ERROR Error: Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays.

The table does however, pick up the columns of the table correctly, just doesn't display any data. I have tried changing the dataSource to a direct array of my items instead of using MatTableDataSource but then I lose the headings and data still isn't displayed.

I would greatly appreciate it if someone can point out to me what I am doing wrong.


UPDATE:

It is now displaying the amount of rows there is in the data but with no heading or any data: Missing elements


Code

The code that is populating my item array is as follows:

  async ngOnInit() {
    // Load the items from the blockchain
    const items = await this.api.getApiRecords();
    this.items = new MatTableDataSource<ApiRecord>(items);
  }

The table itself is defined as follows:

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

  <ng-container matColumnDef="type">
    <mat-header-cell *matHeaderCellDef mat-sort-header> Type </mat-header-cell>
    <mat-cell *matCellDef="let item"> {{item.type}} </mat-cell>
  </ng-container>

  <ng-container matColumnDef="provider">
    <mat-header-cell *matHeaderCellDef mat-sort-header> Provider </mat-header-cell>
    <mat-cell *matCellDef="let item"> {{item.provider}} </mat-cell>
  </ng-container>

  <ng-container matColumnDef="url">
    <mat-header-cell *matHeaderCellDef mat-sort-header> URL </mat-header-cell>
    <mat-cell *matCellDef="let item"> {{item.url}} </mat-cell>
  </ng-container>

  <ng-container matColumnDef="country">
    <mat-header-cell *matHeaderCellDef mat-sort-header> Country </mat-header-cell>
    <mat-cell *matCellDef="let item"> {{item.country}} </mat-cell>
  </ng-container>

  <ng-container matColumnDef="updated_on">
    <mat-header-cell *matHeaderCellDef mat-sort-header> Updated </mat-header-cell>
    <mat-cell *matCellDef="let item"> {{item.updated_on}} </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, 25, 100]"></mat-paginator>

I am getting the following data from my API:

{
    "rows": [
        {
            "id": 0,
            "type": "xxx",
            "provider": "xxx",
            "srvname": "xxx",
            "url": "xxx",
            "continent": "xxx",
            "country": "xxx",
            "flags": 0,
            "revision": 2,
            "updated_on": "2019-03-15T14:03:25",
            "audited_by": "",
            "audited_on": "1970-01-01T00:00:00",
            "audit_ipfs_file": ""
        }
    ],
    "more": false
}

Which I am then serializing into an object that is defined as follows:

export class ApiResponse {
  rows: ApiRecord[];
  more: boolean;
}

Where the ApiRecord is as follows:

export class ApiRecord {
  id: number;
  type: string;
  provider: string;
  srvname: string;
  url: string;
  continent: string;
  country: string;
  flags: number;
  revision: number;
  updated_on: Date;
  audited_by: string;
  audited_on: Date;
  audit_ipfs_file: string;
}
like image 463
Tachyon Avatar asked Dec 18 '25 06:12

Tachyon


1 Answers

I've noticed that, you don't need to wrap MatTableDataSource<> for your API response,

  async ngOnInit() {
    // Load the items from the blockchain
    this.items = await this.api.getApiRecords(); //this will be work
    //this.items = new MatTableDataSource<ApiRecord>(items); << remove this
  }

In this stackblitz example, dataSource variable contains raw data. No need a type for MatTableDataSource<>

Update:

Here is working stackblitz example.

Below code is updated as well,

  <mat-header-row *matHeaderRowDef="['type', 'provider', 'url', 'country', 'updated_on']"></mat-header-row>
  <mat-row *matRowDef="let row; columns: ['type', 'provider', 'url', 'country', 'updated_on']">
  </mat-row>
like image 195
rcanpahali Avatar answered Dec 19 '25 21:12

rcanpahali



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!