【问题标题】:Angular - MatTable not displaying dataAngular - MatTable 不显示数据
【发布时间】:2020-08-07 19:29:57
【问题描述】:

我在显示从我的客户端获取的 JSON 对象之一的数据时遇到问题。我只看到标题数据。我不清楚我错过了什么。

我查看了客户端发送的JSON文件,很好。

TS 文件代码:

import { Component, OnInit, Input } from '@angular/core';
import { MatTableDataSource } from '@angular/material/table';

@Component({
  selector: 'app-list-student',
  templateUrl: './list-student.component.html',
  styleUrls: ['./list-student.component.css']
})
export class ListStudentComponent implements OnInit {
    studentData: any;   
    headerColumns: string[] = ["FIRST_NAME", "LAST_NAME", "DOB_DATE"];
    dataSource = new MatTableDataSource([]);
    summaryTableConfig = [
        {
          'headerValue' : 'First Name', 
          'columnValue' : 'FIRST_NAME',
        },
        {
          'headerValue' : 'Last Name', 
          'columnValue' : 'LAST_NAME',
        },
        {
          'headerValue' : 'DOB', 
          'columnValue' : 'DOB_DATE',
        }
    ];
    
    constructor() { }

  ngOnInit() {
    this.studentData = {"header": {"columnsMap": {"DOB_DATE": "12/12/1942","FIRST_NAME": "JOHN","LAST_NAME": "DOE"}}};
    this.dataSource = new MatTableDataSource;
    this.dataSource.data = this.studentData;
  }
}

HTML 代码

                        <mat-table [dataSource]="dataSource">
                            <ng-container *ngFor="let data of summaryTableConfig" [matColumnDef]="data.columnValue">
                                <mat-header-cell *matHeaderCellDef>
                                    <span>{{ data.headerValue }}</span>
                                </mat-header-cell>
                                <mat-cell  *matCellDef="let element">
                                    <span>{{element["columnsMap"][data.columnValue]}}</span>
                                </mat-cell>
                            </ng-container>
                            <mat-header-row *matHeaderRowDef="headerColumns"></mat-header-row>
                            <mat-row *matRowDef="let row; columns: headerColumns;"></mat-row>
                        </mat-table>

【问题讨论】:

标签: angular typescript datasource mat-table


【解决方案1】:

如果您检查错误日志,您应该会看到 “无法读取未定义的属性 'columnsMap'”。

这就是发生的事情。您使用以下方法初始化 studentData:

{"Students": 
  {"student": 
    [
      {"header": 
        {"columnsMap": 
          {"DOB_DATE": "12/12/1942","FIRST_NAME": "JOHN","LAST_NAME": "DOE"}
        }
      }
    ]
  }
}

然后你通过 this.studentData["Students"]["student"]["header"]["columnsMap"] 遍历 studentData,相当于 this.studentData.Students.student.header.columnsMap。

问题在于 student 是一个数组。因此 this.studentData.Students.student.header 是未定义的,您无法读取未定义的 columnsMap。

为了测试,你可以写 this.studentData.Students.student[0].header.columnsMap。这应该会给你一个有 1 行的表格。

【讨论】:

  • aeberhart,这行得通。我的错误是我得到以下数据到我的组件是 this.studentData = {"header": {"columnsMap": {"DOB_DATE": "12/12/1942","FIRST_NAME": "JOHN","LAST_NAME ": "DOE"}}};我更新了我的问题。
  • 没有,现在我只看到列标题。它没有解决。请检查我更新的代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-08-29
  • 1970-01-01
  • 1970-01-01
  • 2018-09-23
  • 2020-04-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多