2

Below I have added my 'table.component.html' and 'table.component.ts'. I am getting data from an API. I want to populate this data in the data table.

Below I have added my object, which I am recieving from the API)

[
  {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'},
];

Right now I hard coded the objects above but I want to populate the data from the API.

import {MediaMatcher} from '@angular/cdk/layout';
import { Component, OnInit, ViewEncapsulation,ViewChild,ChangeDetectorRef,Inject} from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { ActivatedRoute, Router } from '@angular/router';
import {MatPaginator, MatSort, MatTableDataSource,MatDialog, MatDialogRef, MAT_DIALOG_DATA} from '@angular/material';

@Component({

  selector: 'app-table',

  templateUrl: './table.component.html',

  styleUrls: ['./table.component.css']

})



export class TableComponent {

  displayedColumns = ['position', 'name', 'weight', 'symbol'];

  dataSource = new MatTableDataSource<Element>(ELEMENT_DATA);

   items: any;

  @ViewChild(MatPaginator) paginator: MatPaginator;
  constructor(private router: Router, private route: ActivatedRoute, private http: HttpClient,public dialog: MatDialog) { }
  ngOnInit() {

    this.http.get('/item').subscribe(data => {

      console.log("data>>>>>",data);

      this.items = data;

    });

  }

  ngAfterViewInit() {

    this.dataSource.paginator = this.paginator;

  }

}



export interface Element {

  name: string;

  position: number;

  weight: number;

  symbol: string;

}



const ELEMENT_DATA: Element[] = [
  {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'},
];

My HTML:

<div class="example-container mat-elevation-z8">

  <mat-table #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 #paginator

                 [pageSize]="10"

                 [pageSizeOptions]="[5, 10, 20]">

  </mat-paginator>

</div>

2 Answers 2

3

You need to pass the new data you get from the backend, to the dataSource that will be loaded to the table.

in your component change this:

dataSource = new MatTableDataSource<Element>(ELEMENT_DATA);

to:

dataSource = new MatTableDataSource<Element>();

Then you need to enter the new data into it (assuming your data has the same format as the hard coded one, position, name , weight and symbol):

ngOnInit() {

    this.http.get('/item').subscribe(data => {

      console.log("data>>>>>",data);

      this.items = data;

      this.dataSource.data = this.itens;
    });

  }
Sign up to request clarification or add additional context in comments.

6 Comments

getting error ERROR in src/app/table/table.component.ts(23,15): error TS2314: Generic type 'MatTableDataSource<T>' requires 1 type argument(s).
ok so it must be something like this: dataSource = new MatTableDataSource<Element>(); but now this.items need to be Element type.... what does the console.log prints? you may need to loop the Data it and create an Element with forEach
console i am getting this [ {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'}, ];
can you help me out in this
have you changed the line of the dataSource declaration: dataSource = new MatTableDataSource<Element>(); which error do you get now?
|
0

I would recommend that you take a look into the $http Angular documentation (https://docs.angularjs.org/api/ng/service/$http).

You'll use this to usually make a HTTP request to an API end point. The API end point returns a content that will will be used to instantiate your newly defined class, ready to be used in your view.

2 Comments

The question is about Angular, and not angularjs
Ahh - yep, overlooked that. However similar principles apply.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.