gpt4 book ai didi

Angular Material Table - 异步数据源的问题

转载 作者:行者123 更新时间:2023-12-05 07:28:44 25 4
gpt4 key购买 nike

我正在尝试使用从 AWS DynamoDB 异步加载的数据填充 Angular Material 表,但我能做的最好的事情是使用三个预期项目渲染表,所有返回属性在每个单元格中作为“[object Object]”。

过程是这样的:

  • 在 AllInvoicesComponent.ngOnInit() 中执行 buildTable(),
  • 等待 DataService.getItems('invoices'),
  • 将数据源指定为新的 MatTableDataSource(AllInvoicesComponent.invoices)

'async' 管道对 tabledataSource 属性也完全没有任何作用。

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);
}
})
})
}
}

all-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:
}
}

}

all-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] 属性。

关于Angular Material Table - 异步数据源的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53193405/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com