【问题标题】:Angular Material Table - Issues with async dataSourceAngular Material Table - 异步数据源的问题
【发布时间】:2019-04-11 02:46:13
【问题描述】:

我正在尝试使用从 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>

【问题讨论】:

  • DYNAMODB!这是因为 DynamoDB 毫无意义地将对象的每个属性作为包含值和字段类型作为属性的对象返回。

标签: angular angular-material angular-material2 angular-material-6


【解决方案1】:

您将new MatTableDataSource(this.invoices); 分配给this.dataSource.data,但数据对象只是原始数据。因此,只需将您的代码修复为

this.dataSource = new MatTableDataSource(this.invoices);

你应该很好。

提示: MatTable 数据源不必是 MatTableDataSource 元素。您可以将原始数据传递给 [dataSource] 属性。

【讨论】:

  • 我已经尝试了以上两种方法,结果相同。数据正在加载,结果数量正确,分页器长度正确,但每个单元格都显示“[object Object]”,尽管每个属性都是数字/字符串
  • MatTableDataSource 多用于客户端,应该是一个数组
  • 我在构造函数中获取数据,在 ngOnInit 中我放入了超时函数,它只能以这种方式工作......
猜你喜欢
  • 2020-03-19
  • 2020-11-30
  • 2018-03-26
  • 2017-06-05
  • 1970-01-01
  • 2018-08-01
  • 2021-04-14
  • 1970-01-01
  • 2021-01-12
相关资源
最近更新 更多