【问题标题】:Nested array with mat-table带有 mat-table 的嵌套数组
【发布时间】:2021-11-16 19:03:22
【问题描述】:

我有一个嵌套数组,我想用 mat-table 显示它。

数组中有两个不同的模块“Flex”,它们有几个位置要显示在表格中。

由于数组中有两个“Flex”模块,所以也应该像这个例子一样显示两个表格:

灵活

Position ID Menge Bezeichnung BP_Gesamt
1 STZBH 2 Xpress weiss 1998,00
2 STZBG 5 Xpress schwarz 3998,00

灵活

Position ID Menge Bezeichnung BP_Gesamt
1 STZBH 4 Xpress weiss 3996,00
2 STZBG 4 Xpress schwarz 3996,00
[
{
    "module": "Flex",
    "positionen": [
        {
            "Position": 0,
            "ID": {
                "id": "XLEW_1_2_2",
                "wert": "STZBH"
            },
            "Menge": {
                "id": "XLEW_1_2_5",
                "wert": "2"
            },
            "Bezeichnung": {
                "id": "XLEW_1_2_6",
                "wert": "Xpress weiss"
            },
            "BP_Gesamt": {
                "id": "XLEW_1_2_17",
                "wert": "1998,00"
            }
        },
        {
            "Position": 1,
            "ID": {
                "id": "XLEW_1_2_2",
                "wert": "STZBH"
            },
            "Menge": {
                "id": "XLEW_1_2_5",
                "wert": "5"
            },
            "Bezeichnung": {
                "id": "XLEW_1_2_6",
                "wert": "Xpress weiss"
            },
            "BP_Gesamt": {
                "id": "XLEW_1_2_17",
                "wert": "3998,00"
            }
        }
    ]
},
{
    "module": "Flex",
    "positionen": [
        {
            "Position": 0,
            "ID": {
                "id": "XLEW_1_2_2",
                "wert": "STZBH"
            },
            "Menge": {
                "id": "XLEW_1_2_5",
                "wert": "4"
            },
            "BP_Gesamt": {
                "id": "XLEW_1_2_17",
                "wert": "3996,00"
            }
        },
        {
            "Position": 1,
            "ID": {
                "id": "XLEW_1_2_2",
                "wert": "STZBG"
            },
            "Menge": {
                "id": "XLEW_1_2_5",
                "wert": "4"
            },
            "Bezeichnung": {
                "id": "XLEW_1_2_6",
                "wert": "Xpress schwarz"
            },
            "BP_Gesamt": {
                "id": "XLEW_1_2_17",
                "wert": "3996,00"
            }
        }
    ]
}

]

我是 Angular 和 mat-table 的新手,找不到解决问题的方法。

谁能帮帮我

【问题讨论】:

    标签: angular mat-table


    【解决方案1】:

    你需要在你的项目中安装 underscore.js 然后你才能使用这个代码

    <ng-container *ngFor="let td of table_data">
    <h1>{{td.name}}</h1>
    <table mat-table [dataSource]="td.data" class="mat-elevation-z8">
        <ng-container [matColumnDef]="column" *ngFor="let column of td.header">
            <th mat-header-cell *matHeaderCellDef> {{column}} </th>
            <td mat-cell *matCellDef="let element"> {{element[column]}} </td>
        </ng-container>
    
        <tr mat-header-row *matHeaderRowDef="td.header"></tr>
        <tr mat-row *matRowDef="let row; columns: td.header;"></tr>
    </table>
    
    parseData(inputArray: any[]) {
        const returnArray = [];
        for (const data of inputArray) {
            const new_obj = { name: "", header: [], data: [] };
            const arr = [];
            new_obj["data"] = [];
            new_obj["name"] = data.module;
            for (const d of data.positionen) {
                const keys = Object.keys(d);
                const obj: any = {};
                for (const k of keys) {
                    if (k === "Position") {
                        obj[k] = d[k];
                    } else {
                        obj[k] = d[k].wert;
                    }
                }
                new_obj["data"].push(obj);
                arr.push(keys);
            }
            new_obj["header"] = _.uniq(_.flatten(arr));
            returnArray.push(new_obj);
        }
    
        return returnArray;
    }
    
    ngOnInit(): void {
        this.table_data = this.parseData(PASS_YOUR_DATA_HERE);
    }
    

    【讨论】:

    • 你是我的男人!非常感谢!完美运行
    猜你喜欢
    • 2021-02-27
    • 2015-06-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多