[英]How can i populate data from api using angular5 material2?
Below I have added my 'table.component.html' and 'table.component.ts'. 在下面,我添加了“ table.component.html”和“ table.component.ts”。 I am getting data from an API.
我正在从API获取数据。 I want to populate this data in the data table.
我想在数据表中填充此数据。
Below I have added my object, which I am recieving from the API) 在下面,我添加了我从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. 现在,我对上面的对象进行了硬编码,但是我想从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: 我的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>
You need to pass the new data you get from the backend, to the dataSource that will be loaded to the table. 您需要将从后端获取的新数据传递到将要加载到表的dataSource中。
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;
});
}
I would recommend that you take a look into the $http Angular documentation (
https://docs.angularjs.org/api/ng/service/ $http).
我建议您看一下$ http Angular文档(
https://docs.angularjs.org/api/ng/service/ $ http)。
You'll use this to usually make a HTTP request to an API end point. 您通常会用它来向API端点发出HTTP请求。 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.
API端点返回的内容将用于实例化新定义的类,准备在视图中使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.