David Weng
David Weng

Reputation: 45

With ui-grid, how to access $scope from nested rows?

On the first level row, I can access $scope by grid.appScope. But in the second level row, I have a clue what grid.appScope returns.

I have a checkbox in a column of the second level row, when a user clicks, I need to call a method. On the first level row, I can do like below

$scope.update_field = function...

then in columndef, I add ng-click="grid.appScope.update_field...

How do I do the similar on the second level row?

Below is the code, now, the checkfield works, but cannot make subcheckfield work.

controller('ctrlProductAssignment', function ($scope, $controller, $routeParams, api, libs, options, gui) {

    $scope.update_field = function (rowEntity, colDef, newValue, oldValue) {
        if (rowEntity.id > 0)
          ....
    };

    $scope.gridOptions = {
        onRegisterApi: function (gridApi) {
            $scope.gridApi = gridApi;

            gridApi.expandable.on.rowExpandedStateChanged($scope, function (row) {
                if (row.isExpanded) {
                    row.entity.subGridOptions = {
                        columnDefs: [
                             { name: 'subcheckfield', displayName: 'Registered', 
                              cellTemplate: '<input type="checkbox" ng-model="row.entity[col.name]" ng-click="grid.appScope.update_field(row.entity, { name:col.name }, row.entity[col.name], 0)">' },
                          ]
                    };

                    api.raffle_productassignmentdetail.query({ raffle_id: $scope.deal.id, pa_id: row.entity.id }).$promise.then(function (response) {
                        row.entity.subGridOptions.data = response;
                    });
                }
            });
        },

        columnDefs: [
         { name: 'checkfield', displayName: 'Registered',                               
           cellTemplate: '<input type="checkbox" ng-model="row.entity[col.name]" ng-click="grid.appScope.update_field(row.entity, { name:col.name }, row.entity[col.name], 0)">' },
        ]
    };

    api.raffle_productassignment.query({ raffle_id: $scope.deal.id, reseller_id: reseller_id }).$promise.then(function (response) {
        $scope.gridOptions.data = response;
    });
}

Upvotes: 0

Views: 1264

Answers (1)

RRoquais
RRoquais

Reputation: 91

The nested rows have their own scope, and their parent is the scope of the parent row. In your example, just write down ng-click="grid.appScope.scope.update_field and it will work.

Upvotes: 1

Related Questions