下面我添加了“table.component.html”和“table.component.ts”。 我正在从 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'},
];
现在我对上面的对象进行了硬编码,但我想从 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'},
];
我的 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>
最佳答案
您需要将从后端获取的新数据传递给将加载到表中的数据源。
在您的组件中更改此:
dataSource = new MatTableDataSource<Element>(ELEMENT_DATA);
至:
dataSource = new MatTableDataSource<Element>();
然后您需要在其中输入新数据(假设您的数据与硬编码的数据具有相同的格式,位置,名称,重量和符号):
ngOnInit() {
this.http.get('/item').subscribe(data => {
console.log("data>>>>>",data);
this.items = data;
this.dataSource.data = this.itens;
});
}
关于javascript - 如何使用 Angular5 Material2 从 api 填充数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48304755/