Reputation:
How can I pass my API service data in Angular Material DataTable. I want to render my data instead of this static array. If I render my data like *ngFor="let item of service.list"
so the data is loaded but pagination and sorting is not working. Please give some solution or example for it.
import { Component, OnInit, ViewChild } from '@angular/core';
import { MatPaginator, MatSort, MatTableDataSource } from '@angular/material';
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'}
];
@Component({
selector: 'app-root',
styleUrls: ['./app.component.css'],
templateUrl: './app.component.html',
})
export class AppComponent implements OnInit {
displayedColumns: string[] = ['position', 'name', 'weight', 'symbol'];
dataSource = new MatTableDataSource(ELEMENT_DATA);
@ViewChild(MatPaginator) paginator: MatPaginator;
@ViewChild(MatSort) sort: MatSort;
ngOnInit() {
this.dataSource.paginator = this.paginator;
this.dataSource.sort = this.sort;
}
}
Upvotes: 6
Views: 3282
Reputation: 10697
Try this, with Dynamic column names for Table:
HTML Code:
<table mat-table [dataSource]="dataSource" class="mat-elevation-z8" matSort>
<ng-container [matColumnDef]="column" *ngFor="let column of displayedColumns">
<th mat-header-cell *matHeaderCellDef mat-sort-header> {{column}} </th>
<td mat-cell *matCellDef="let element"> {{element[column]}} </td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
</table>
<mat-paginator [length]="dataSource.length" [pageSize]="10" [pageIndex]="0" [pageSizeOptions]="[3,5,10]" showFirstLastButtons></mat-paginator>
TS Code:
import { DataService } from './data.service';
import { Component, OnInit, ViewChild } from '@angular/core';
import { MatPaginator, MatSort, MatTableDataSource } from '@angular/material';
/**
* @title Basic use of `<table mat-table>`
*/
@Component({
selector: 'table-basic-example',
styleUrls: ['table-basic-example.css'],
templateUrl: 'table-basic-example.html',
providers: [DataService]
})
export class TableBasicExample implements OnInit {
displayedColumns = []
dataSource = new MatTableDataSource([]);
@ViewChild(MatPaginator) paginator: MatPaginator;
@ViewChild(MatSort) sort: MatSort;
constructor(private service: DataService) {
}
ngOnInit() {
this.service.getList().then(res => {
this.displayedColumns = Object.keys(res[0])
this.dataSource = new MatTableDataSource(res);
this.dataSource.paginator = this.paginator;
this.dataSource.sort = this.sort;
})
}
}
data.service.ts
:
import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import { HttpClient, HttpHeaders } from '@angular/common/http';
@Injectable()
export class DataService {
constructor(private http: HttpClient
) { }
private url = "https://jsonplaceholder.typicode.com/todos";
getList(): Promise<any> {
const url = `${this.url}`;
return this.http.get(url)
.toPromise()
.catch(this.handleError);
}
// handler for error in URL
private handleError(error: any): Promise<any> {
return Promise.reject(error.message || error);
}
}
Upvotes: 4