4

This answer states that this code:

$scope.gridOptions.onRegisterApi = function(gridApi){
  $scope.gridApi = gridApi;
  $scope.mySelectedRows=$scope.gridApi.selection.getSelectedRows();
}

Should work in order to get the selected rows, but to me it returns always [], to get track of selected rows I have to call gridApi.selection.getSelectedRows() each time a selection event is triggered, is this correct ?

What I want to achieve is to do my own footer that tracks the number of selected rows of the grid, is this the correct way of achieving this ?

KARTHIKEYAN.A
  • 18,210
  • 6
  • 124
  • 133
kitensei
  • 2,510
  • 2
  • 42
  • 68

4 Answers4

6

I have it working w/o having to use the event trigger. I added a function, tied it to a button and can retrieve selected items only when I need them.

$scope.gridOptions = {
  data: 'data',
  enableRowSelection: true,
  onRegisterApi: function(gridApi) { //register grid data first within the gridOptions
    $scope.gridApi = gridApi;
  }
};
//this is the on click function
$scope.getCurrentSelection = function() {
  var currentSelection = $scope.gridApi.selection.getSelectedRows();
  console.log(currentSelection);
};
Roman K
  • 394
  • 4
  • 5
5

There is already an example of showing the number of selected elements in the footer.

This plnkr shows the selected items footer. http://plnkr.co/edit/jc1YPCXBmfOKWyu8sLkx?p=preview

If you want to do further analysis on the selected row you can register a listener for the row selection and act on that.

 $scope.gridOptions.onRegisterApi = function(gridApi){
      //set gridApi on scope
      $scope.gridApi = gridApi;
      gridApi.selection.on.rowSelectionChanged($scope,function(row){
        var msg = 'row selected ' + row.isSelected;
        $log.log(msg);
      });

      gridApi.selection.on.rowSelectionChangedBatch($scope,function(rows){
        var msg = 'rows changed ' + rows.length;
        $log.log(msg);
      });
    };
Kathir
  • 6,136
  • 8
  • 36
  • 67
  • that's indeed the way I did it finally, registering the rowSelectionChangedEvent and tracking the number of selected items – kitensei May 22 '15 at 06:15
0

This function is work for me when selectAll function activated i have get the all selected rows.

gridApi.selection.on.rowSelectionChangedBatch($scope,function(rows){
                    console.log(row);
                  });
KARTHIKEYAN.A
  • 18,210
  • 6
  • 124
  • 133
0
$scope.gridOptions = {
  data: 'data',
  enableRowSelection: true,
  onRegisterApi: function(gridApi) { //register grid data first within the gridOptions
    $scope.gridApi = gridApi;
  }
};

After you have access to the variable {{vm.gridApi.selection.getSelectedRows().length}} in template html directily.

elciospy
  • 260
  • 4
  • 11