Angular Material Table - 异步数据源的问题
Angular Material Table - Issues with async dataSource
我正在尝试使用从 AWS DynamoDB 异步加载的数据填充 Angular Material table,但我能做的最好的事情就是将 table 渲染为三个预期项目将每个单元格中的属性作为“[object Object]”返回。
流程是这样的:
- 在 AllInvoicesComponent.ngOnInit() 处,执行 buildTable(),
- 等待DataService.getItems('invoices'),
- 将数据源指定为新的 MatTableDataSource(AllInvoicesComponent.invoices)
'async' 管道对 table
的 dataSource
属性也完全没有任何作用。
data.service.ts:
import { Injectable } from '@angular/core';
import * as aws from 'aws-sdk';
import { Observable } from 'rxjs';
import AuthService from '@app/services/auth.service';
@Injectable({
providedIn: 'root'
})
export class DataService {
dynamodb;
docClient;
constructor(private auth: AuthService) {
aws.config.credentials = new aws.Credentials(auth.credentials.accessKeyId, auth.credentials.secretAccessKey, null);
aws.config.update({
region: 'eu-west-1'
})
this.dynamodb = new aws.DynamoDB();
this.docClient = new aws.DynamoDB.DocumentClient();
}
async getItems(tableName) {
const params = {
TableName: tableName
}
return new Promise((resolve, reject) => {
this.dynamodb.scan(params, (err, data) => {
if (err) {
reject(err);
} else {
resolve(data.Items);
}
})
})
}
}
全部-invoices.component.ts:
import { Component, OnInit, ViewChild } from '@angular/core';
import { Observable, from } from 'rxjs';
import {MatPaginator, MatSort, MatTableDataSource} from '@angular/material';
import Invoice from '../invoice.interface';
import { DataService } from '../../services/data/data.service';
@Component({
selector: 'app-all-invoices',
templateUrl: './all-invoices.component.html',
styleUrls: ['./all-invoices.component.scss']
})
export class AllInvoicesComponent implements OnInit {
invoices;
dataSource: MatTableDataSource<any>;
@ViewChild(MatPaginator) paginator: MatPaginator;
@ViewChild(MatSort) sort: MatSort;
tableColumns = [
'number',
'reference'
]
constructor(private database: DataService) {
}
ngOnInit() {
this.buildTable();
}
async buildTable() {
try {
this.invoices = await this.database.getItems('invoices');
this.dataSource = new MatTableDataSource(this.invoices);
} catch(err) {
console.error(`Error retrieving invoices: ${err.Message}`);
// TODO:
}
}
}
全部-invoices.component.html:
<p>All Invoices</p>
<br>
<div class="spinner-container" *ngIf="dataSource.loading$ | async">
<mat-spinner></mat-spinner>
</div>
<table mat-table [dataSource]="dataSource" matSort>
<ng-container matColumnDef="number">
<th mat-header-cell *matHeaderCellDef mat-sort-header>Number</th>
<td mat-cell *matCellDef="let invoice">{{invoice.number}}</td>
</ng-container>
<ng-container matColumnDef="reference">
<th mat-header-cell *matHeaderCellDef mat-sort-header>Reference</th>
<td mat-cell *matCellDef="let invoice">{{invoice.reference}}</td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="tableColumns"></tr>
<tr mat-row *matRowDef="let row; columns: tableColumns">
</table>
<mat-paginator [length]="dataSource.length" [pageSize]="25" [pageSizeOptions]="[5, 10, 25, 50]"></mat-paginator>
您正在将 new MatTableDataSource(this.invoices);
分配给 this.dataSource.data
,但数据对象只是原始数据。所以只需将您的代码修改为
this.dataSource = new MatTableDataSource(this.invoices);
你应该很好。
蒂普:
MatTable 数据源不必是 MatTableDataSource 元素。您可以将原始数据传递给 [dataSource] 属性。
我正在尝试使用从 AWS DynamoDB 异步加载的数据填充 Angular Material table,但我能做的最好的事情就是将 table 渲染为三个预期项目将每个单元格中的属性作为“[object Object]”返回。
流程是这样的:
- 在 AllInvoicesComponent.ngOnInit() 处,执行 buildTable(),
- 等待DataService.getItems('invoices'),
- 将数据源指定为新的 MatTableDataSource(AllInvoicesComponent.invoices)
'async' 管道对 table
的 dataSource
属性也完全没有任何作用。
data.service.ts:
import { Injectable } from '@angular/core';
import * as aws from 'aws-sdk';
import { Observable } from 'rxjs';
import AuthService from '@app/services/auth.service';
@Injectable({
providedIn: 'root'
})
export class DataService {
dynamodb;
docClient;
constructor(private auth: AuthService) {
aws.config.credentials = new aws.Credentials(auth.credentials.accessKeyId, auth.credentials.secretAccessKey, null);
aws.config.update({
region: 'eu-west-1'
})
this.dynamodb = new aws.DynamoDB();
this.docClient = new aws.DynamoDB.DocumentClient();
}
async getItems(tableName) {
const params = {
TableName: tableName
}
return new Promise((resolve, reject) => {
this.dynamodb.scan(params, (err, data) => {
if (err) {
reject(err);
} else {
resolve(data.Items);
}
})
})
}
}
全部-invoices.component.ts:
import { Component, OnInit, ViewChild } from '@angular/core';
import { Observable, from } from 'rxjs';
import {MatPaginator, MatSort, MatTableDataSource} from '@angular/material';
import Invoice from '../invoice.interface';
import { DataService } from '../../services/data/data.service';
@Component({
selector: 'app-all-invoices',
templateUrl: './all-invoices.component.html',
styleUrls: ['./all-invoices.component.scss']
})
export class AllInvoicesComponent implements OnInit {
invoices;
dataSource: MatTableDataSource<any>;
@ViewChild(MatPaginator) paginator: MatPaginator;
@ViewChild(MatSort) sort: MatSort;
tableColumns = [
'number',
'reference'
]
constructor(private database: DataService) {
}
ngOnInit() {
this.buildTable();
}
async buildTable() {
try {
this.invoices = await this.database.getItems('invoices');
this.dataSource = new MatTableDataSource(this.invoices);
} catch(err) {
console.error(`Error retrieving invoices: ${err.Message}`);
// TODO:
}
}
}
全部-invoices.component.html:
<p>All Invoices</p>
<br>
<div class="spinner-container" *ngIf="dataSource.loading$ | async">
<mat-spinner></mat-spinner>
</div>
<table mat-table [dataSource]="dataSource" matSort>
<ng-container matColumnDef="number">
<th mat-header-cell *matHeaderCellDef mat-sort-header>Number</th>
<td mat-cell *matCellDef="let invoice">{{invoice.number}}</td>
</ng-container>
<ng-container matColumnDef="reference">
<th mat-header-cell *matHeaderCellDef mat-sort-header>Reference</th>
<td mat-cell *matCellDef="let invoice">{{invoice.reference}}</td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="tableColumns"></tr>
<tr mat-row *matRowDef="let row; columns: tableColumns">
</table>
<mat-paginator [length]="dataSource.length" [pageSize]="25" [pageSizeOptions]="[5, 10, 25, 50]"></mat-paginator>
您正在将 new MatTableDataSource(this.invoices);
分配给 this.dataSource.data
,但数据对象只是原始数据。所以只需将您的代码修改为
this.dataSource = new MatTableDataSource(this.invoices);
你应该很好。
蒂普: MatTable 数据源不必是 MatTableDataSource 元素。您可以将原始数据传递给 [dataSource] 属性。