Angularjs ui.grid显示嵌套行

时间:2015-12-20 12:11:01

标签: javascript angularjs angular-ui-grid

我一直在尝试在列中获取嵌套的json数据。 json结构是:

[{
    "ratings": [{
            "rating": "Detractor",
            "employees": [{
                "empName": "Pavan",
                "quarters": [{
                    "quarterName": "q1 2015",
                    "weeks": [{
                            "weekName": "week1",
                            "month": "January",
                            "points": [{
                                "0": "point1"
                            }, {
                                "1": "point2"
                            }]
                        }
                        , {
                            "weekName": "week2",
                            "month": "January",
                            "points": [{
                                "0": "point1"
                            }, {
                                "1": "point2"
                            }]
                        }, {
                            "weekName": "week3",
                            "month": "January",
                            "points": [{
                                "0": "point1"
                            }, {
                                "1": "point2"
                            }]
                        }
                    ]
                }, {
                    "quarterName": "q2 2015",
                    "weeks": [{
                            "weekName": "week4",
                            "month": "January",
                            "points": [{
                                "0": "point1"
                            }, {
                                "1": "point2"
                            }]
                        }
                        , {
                            "weekName": "week5",
                            "month": "January",
                            "points": [{
                                "0": "point1"
                            }, {
                                "1": "point2"
                            }]
                        }, {
                            "weekName": "week6",
                            "month": "January",
                            "points": [{
                                "0": "point1"
                            }, {
                                "1": "point2"
                            }]
                        }
                    ]
                }]
            }]
        }
    ]
}]

输出应该在一列中具有评级,在下一列中具有相应的雇员,在第三列中具有点。 我找到了一个解决方案here,但它只涉及一个级别,我无法将其扩展到多个级别。

我需要显示每周数据。

Expected Output

1 个答案:

答案 0 :(得分:0)

一般来说,在ui-grid中访问嵌套对象没有问题。

在您的情况下,问题似乎与您拥有动态结构的事实更为相关。

在ui-grid中,您可以为列分配一个值,该字符串包含该值的完整路径,例如:

  • 假设您将评级子阵列分配给gridOptions.datafieldweek1week2的{​​{1}}值将分别为:
    • week4
    • 'employees[0].quarters[0].weeks[0].points[0]' 'employees[0].quarters[0].weeks[ 1
    • ].points[0]' 'employees[0].quarters[ 1

现在,添加周数和周数并不固定,您会发现没有直接的方法以编程方式将周数与周数和季度数组所需的索引相关联。

我认为处理此问题的最佳方法是迭代您收到的原始数据,并构建一个更适合网格的新JSON。

在伪代码中,您可以执行以下操作:

].weeks[0].points[0]'