【发布时间】:2020-06-12 23:32:17
【问题描述】:
我正在尝试将数据加载到 mat-table 中,但是当我 console.log 我尝试加载的数据源时,它说数据未定义。
我检索到的对象是嵌套的。 API Request
我正在关注 Angular 网站上的官方教程,它是 material table examples。 也可以从这里访问Table retrieving data through HTTP
回复:
{"BTC":{"USD":8682.55,"EUR":7851.55},"ETH":{"USD":224.8,"EUR":203.77}}
这是我迄今为止的尝试: (你会注意到,ngAfterViewInit() 没有被使用,那是由于用户输入 Api 必须等待,我还在修改它。
HTML:
<button (click)="removeList()">Reset</button>
<button (click)="initialiseTableData()">Display Value</button>
<div class="example-container mat-elevation-z8">
<div class="example-loading-shade" *ngIf="isLoadingResults">
<mat-spinner *ngIf="isLoadingResults"></mat-spinner>
</div>
<mat-table #table [dataSource]="dataSource" class="example-table" matSort matSortActive="created" matSortDisableClear
matSortDirection="asc">
<ng-container matColumnDef="cryptocurrency">
<mat-header-cell *matHeaderCellDef>Cryptocurrency</mat-header-cell>
<mat-cell *matCellDef="let row">{{ row.items.cryptovalue }}</mat-cell>
</ng-container>
<ng-container matColumnDef="currency">
<mat-header-cell *matHeaderCellDef>Currency</mat-header-cell>
<mat-cell *matCellDef="let row">{{ row.items.currency }}</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 [length]="resultsLength" [pageSize]="30">
</mat-paginator>
</div>
这是我的 .ts :
import { Component, OnInit, AfterViewInit, ViewChild } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { merge, Observable, of as observableOf } from 'rxjs';
import { catchError, map, startWith, switchMap } from 'rxjs/operators';
import { FormControl } from '@angular/forms';
import { MatSortModule, MatSort } from '@angular/material/sort';
import { MatPaginatorModule, MatPaginator } from '@angular/material/paginator';
import { MatTableDataSource } from '@angular/material/table';
export interface TableDataApi {
items: Tabledata[];
total_count: number;
}
export interface Tabledata {
cryptovalue: {
currency: string;
};
}
@Component({
selector: 'app-crypto-chooser',
templateUrl: './crypto-chooser.component.html',
styleUrls: ['./crypto-chooser.component.css']
})
export class CryptoChooserComponent implements OnInit, AfterViewInit {
constructor(private client: HttpClient) { }
displayedColumns = ['cryptocurrency', 'currency'];
dataSource = new MatTableDataSource();
resultsLength = 0;
isLoadingResults = false;
@ViewChild(MatPaginator) paginator: MatPaginator;
@ViewChild(MatSort) sort: MatSort;
initialiseTableData() {
this.sort.sortChange.subscribe(() => this.paginator.pageIndex = 0);
merge(this.sort.sortChange, this.paginator.page)
.pipe(
startWith({}),
switchMap(() => {
this.isLoadingResults = true;
return this.getCoins();
}),
map(data => {
this.isLoadingResults = false;
this.resultsLength = data.total_count;
return data.items;
}),
catchError(() => {
this.isLoadingResults = false;
return observableOf([]);
})
).subscribe(data => this.dataSource.data = data);
console.log(this.dataSource);
}
ngAfterViewInit() {
}
getCoins(): Observable<TableDataApi> {
this.generateUrl();
return this.client.get<TableDataApi>(this.urlComplete, this.httpHeader);
}
【问题讨论】:
-
尝试在订阅方法上打印您的数据。这样做
.subscribe(data => {console.log(data); this.dataSource.data = data;});你能这样看你的数据吗? -
用“新的 MatTableDataSource();” ,它只会给我“未定义”。
-
如果您使用我在
console.log(data);之前注释的代码片段显示未定义?还是显示 API 负载? -
是的,使用 dataSource: Tabledata[] = [],它给了我 undefined。
-
你能在地图运算符上打印数据变量吗?
map(data => { console.info('what is data?', data); this.isLoadingResults = false; this.resultsLength = data.total_count; return data.items; }),
标签: angular api undefined datasource mat-table