17

I'm using ui-grid. But I cannot get the $scope.gridApi from download function. And get the error: Cannot read property 'selection' of undefined. Somebody can tell me the reason? thanks.

    $scope.gridOptions.onRegisterApi = function (gridApi) {
        $log.info('gridApi...');
        $scope.gridApi = gridApi;
        $log.info($scope.gridApi);
    };
    $scope.download = function ($event) {
        $event.stopPropagation();
        var selectedRows = $scope.gridApi.selection.getSelectedRows();
        $log.log('selectedRows....');
        $log.log(selectedRows);
    };
Charles
  • 171
  • 1
  • 1
  • 4

3 Answers3

9

There are three potential causes:

  • firstly, not including selection properly, either not including the module in your js code as a dependency, or not including the directive on your grid definition in your html. This will result in having a gridApi, but no gridApi.selection
  • secondly, trying to call this before onRegisterApi has fired, or defining onRegisterApi on your gridOptions after onRegisterApi has already fired. This will result in the method not being called, and therefore $scope.gridApi being undefined, which from your error text sounds like what the problem is.
  • lastly, problems with your invocation that means the $scope of your download call is different than the $scope you put the gridApi on. This shouldn't really happen, but I guess it could. You could tell this by putting a breakpoint in the method to see what $scope it has.
PaulL
  • 6,650
  • 3
  • 35
  • 39
6

I resolved this issue after checking:

  • ui-grid css loaded (ui-grid.min.css)
  • ui-grid javascript library loaded (ui-grid.min.js)
  • 'ui.grid' and 'ui.grid.selection' added to module initialization app = angular.module('app',['ui.grid','ui.grid.selection'])` )
  • adding the ui-grid-selection directive to the ui-grid markup
    <div id="my-grid" ui-grid="gridOptions" ui-grid-selection ></div>
F.Igor
  • 4,119
  • 1
  • 18
  • 26
  • 2
    I was not including ui.grid.selection in app.js or adding the ui-grid-selection directive. Adding these items fixed my issue. – Ian Jul 10 '18 at 13:32
0

the answers above are very hepful. But in my case I resolve it after removing : - ui-grid.core.min.js - ui-grid.cellnav.min.js - ui-grid.selection.min.js

from my loading js and keeping only ui-grid.min.js.

Ollo Kambou
  • 1
  • 1
  • 1