如何在角度2中循环一个td

时间:2018-06-11 07:57:58

标签: javascript angular ngfor

您好,我的数据结构如下:

{

"status": {
    "code": null,
    "message": null,
    "status": 200,
    "parameters": null
},
"response": {
    "id": 63,
    "years": [{
        "year": 2018
    }, {
        "year": 2017
    }],
    "createdBy": "System",
    "createdOn": 1528448882000,
    "modifiedBy": "System",
    "modifiedOn": 1528448882000,
    "uploaded": [{
            "id": 97,
            "createdBy": "System",
            "createdOn": 1528448883000,
            "modifiedBy": "System",
            "modifiedOn": 1528448883000,
            "year": 2017,
            "fileName": "file2.pdf"
        {
            "id": 97,
            "createdBy": "System",
            "createdOn": 1528448883000,
            "modifiedBy": "System",
            "modifiedOn": 1528448883000,
            "year": 2017,
            "fileName": null,
        },
        {
            "id": 97,
            "createdBy": "System",
            "createdOn": 1528448883000,
            "modifiedBy": "System",
            "modifiedOn": 1528448883000,
            "year": 2018,
            "fileName": file4.pdf,
        }
    ],
    "uploadedFromTS": [{
        "id": 97,
        "createdBy": "System",
        "createdOn": 1528448883000,
        "modifiedBy": "System",
        "modifiedOn": 1528448883000,
        "year": 2017,
        "fileName": "file1.pdf",
    }, {
        "id": 97,
        "createdBy": "System",
        "createdOn": 1528448883000,
        "modifiedBy": "System",
        "modifiedOn": 1528448883000,
        "year": 2017,
        "fileName": "file3.pdf",
    }]
}

}

我正在根据以下数据构建一个表格:

------------------------------------
category -  filename - created by
------------------------------------
2017        file1.pdf     Sasi
            file2.pdf     sambhu
            file3.pdf     Madhavan
------------------------------------
2018        file4.pdf 
             -
             -
------------------------------------

以下是我的角度代码

this._svc.getUploadedfiles(parseInt(this.id)).then((data: any) => {
                    if (data.response) {
                        if (data.response.uploadedFiles) {
                            if (data.response.uploadedFromTS.length > 0) {
                                this.tableList = data.response.uploadedFiles.concat(data.response.uploadedFromTS);
    }
    ...

如何以这样的方式对数据进行分组,以便我可以如表中所示显示它们。目前我只是盲目地显示所有记录。所以今年一直在我的第一栏重复。我怎样才能将它们分组以实现它们呢?提前致谢

1 个答案:

答案 0 :(得分:0)

您可以对原始数据进行一些修改。

您可以通过分组(按年份)重新排列数据。

有些事情是这样的:

,例如,这是您的数据

let test =  [{
            "id": 97,
            "createdBy": "System",
            "createdOn": 1528448883000,
            "modifiedBy": "System",
            "modifiedOn": 1528448883000,
            "year": 2017,
            "fileName": "file2.pdf"},
        {
            "id": 97,
            "createdBy": "System",
            "createdOn": 1528448883000,
            "modifiedBy": "System",
            "modifiedOn": 1528448883000,
            "year": 2017,
            "fileName": null,
        },
        {
            "id": 97,
            "createdBy": "System",
            "createdOn": 1528448883000,
            "modifiedBy": "System",
            "modifiedOn": 1528448883000,
            "year": 2018,
            "fileName": "xwe",
        }
    ]

你可以按年份分组:

   let result = test.reduce(function (r, a) {
            r[a.year] = r[a.year] || [];
            r[a.year].push(a);
            return r;
        }, Object.create(null));

你会得到你需要的结果!

enter image description here