使用ngFor遍历二维数组

时间:2022-03-08 21:36:31

I've been beating my head up against the wall on this one for a while but I finally feel close. What I'm trying to do is read my test data, which goes to a two dimensional array, and print its contents to a table in the html, but I can't figure out how to use an ngfor to loop though that dataset

我一直在这个墙上撞到墙上一段时间,但我终于感到亲近了。我正在尝试做的是读取我的测试数据,该数据转到二维数组,并将其内容打印到html中的表中,但我无法弄清楚如何使用ngfor来循环该数据集

Here is my typescript file

这是我的打字稿文件

import { Component } from '@angular/core';
import { Http } from '@angular/http';

@Component({
    selector: 'fetchdata',
    template: require('./fetchdata.component.html')
})
export class FetchDataComponent {
    public tableData: any[][];

    constructor(http: Http) {

        http.get('/api/SampleData/DatatableData').subscribe(result => {
            //This is test data only, could dynamically change
            var arr = [
                { ID: 1, Name: "foo", Email: "foo@foo.com" },
                { ID: 2, Name: "bar", Email: "bar@bar.com" },
                { ID: 3, Name: "bar", Email: "bar@bar.com" }
            ]
            var res = arr.map(function (obj) {
                return Object.keys(obj).map(function (key) {
                    return obj[key];
                });
            });

            this.tableData = res;
            console.log("Table Data")
            console.log(this.tableData)
        });
    }
}

Here is my html which does not work at the moment

这是我的html,目前无效

<p *ngIf="!tableData"><em>Loading...</em></p>

<table class='table' *ngIf="tableData">
    <tbody>
        <tr *ngFor="let data of tableData; let i = index">
            <td>
            {{ tableData[data][i] }}
            </td>

        </tr>
    </tbody>
</table>

Here is the output from my console.log(this.tableData) 使用ngFor遍历二维数组

这是我的console.log(this.tableData)的输出

My goal is to have it formatted like this in the table

我的目标是在表格中将其格式化为这样

1 | foo | bar@foo.com
2 | bar | foo@bar.com

Preferably I'd like to not use a model or an interface because the data is dynamic, it could change at any time. Does anyone know how to use the ngfor to loop through a two dimensional array and print its contents in the table?

我最好不要使用模型或界面,因为数据是动态的,它可能随时改变。有谁知道如何使用ngfor循环二维数组并在表中打印其内容?

1 个解决方案

#1


2  

Like Marco Luzzara said, you have to use another *ngFor for the nested arrays.

就像Marco Luzzara所说,你必须为嵌套数组使用另一个* ngFor。

I answer this just to give you a code example:

我回答这个问题只是为了给你一个代码示例:

<table class='table' *ngIf="tableData">
    <tbody>
        <tr *ngFor="let data of tableData; let i = index">
            <td *ngFor="let cell of data">
              {{ cell }}
            </td>
        </tr>
    </tbody>
</table>

#1


2  

Like Marco Luzzara said, you have to use another *ngFor for the nested arrays.

就像Marco Luzzara所说,你必须为嵌套数组使用另一个* ngFor。

I answer this just to give you a code example:

我回答这个问题只是为了给你一个代码示例:

<table class='table' *ngIf="tableData">
    <tbody>
        <tr *ngFor="let data of tableData; let i = index">
            <td *ngFor="let cell of data">
              {{ cell }}
            </td>
        </tr>
    </tbody>
</table>