如何在可扩展网格中设置子网格的数据

时间:2017-07-18 09:46:11

标签: javascript angularjs ui-grid

我的数组就像这样

$scope.ebObjArr = [{key: 'val', monthsArray: [{myDate:'',}]},{myDate:'',}]

monthArray用于填充子网格。我无法弄清楚如何为子网格设置数据。

我试过了:

 for (let i = 0; i < $scope.ebObjArr.length; i++) {
    $scope.ebObjArr.monthsArray[i].subGridOptions = {
        columnDefs: [{ name: "Month", field: "myDate" }, { name: "Cost", field: "totalCost" }, {name:"Units consumed", field : "unitsConsumed"}],
        data: $scope.ebObjArr[i].monthsArray
    }
}

我也试过了:

for(let i = 0; i&lt; $ scope.ebObjArr.length; i ++){

                for (let j = 0; j < $scope.ebObjArr[i].monthsArray[j].length; j++) {
                    $scope.ebObjArr[i].monthsArray[j].subGridOptions = {
                        columnDefs: [{ name: "Month", field: "myDate" }, { name: "Cost", field: "totalCost" }, { name: "Units consumed", field: "unitsConsumed" }],
                        data: $scope.ebObjArr[i].monthsArray[j]
                    }
                }


            }

我在此示例中仅使用MainCtrlhttp://ui-grid.info/docs/#/tutorial/216_expandable_grid

1 个答案:

答案 0 :(得分:0)

我确定有更好的方法可以做到这一点,但由于没有人回答我做了这件事并且有效。

        onRegisterApi: function (gridApi) {
        $scope.grid1Api = gridApi;
        $scope.grid1Api.expandable.on.rowExpandedStateChanged($scope, function (row) {
            if (row.isExpanded) {
                console.log(row.entity);
                row.entity.subGridOptions = {
                    columnDefs: [{ name: "Month", field: "myDate" }, { name: "Cost", field: "totalCost" }, { name: "Units consumed", field: "unitsConsumed" }],
                }
                row.entity.subGridOptions.data = (function () {
                    var a;

                         a = $scope.ebObjArr.filter(function (val) {
                            if (val.ebName === row.entity.ebName && val.ebAddress === row.entity.ebAddress) {
                               // console.log(val.monthsArray);
                                return val.monthsArray
                            }
                            return false;
                         });
                         console.log(a);
                    console.log(a[0].monthsArray);
                    return a[0].monthsArray;
                })();
                console.log(row.entity.subGridOptions.data);
            }

        });
    }