1

I want to select all rows of a grid by default, and I managed to do so by adding a data listener in onRegisterApi as specified in this answer:

onRegisterApi : function(gridApi)
    {
      $scope.gridApi = gridApi; 
      $scope.gridApi.grid.registerDataChangeCallback(function(data)
        {
          $scope.gridApi.selection.selectRow($scope.gridOptions.data[0]);
        }, [uiGridConstants.dataChange.ROW]);
    }

Working plunkr: http://plnkr.co/edit/dzf6PZwKdZmSNvKzQeYH?p=preview

However, I don't understand why it doesn't work without a listener, like

onRegisterApi : function(gridApi)
    {
      $scope.gridApi = gridApi; 
        _.each($scope.gridOptions.data, function(companies, index){
          $scope.gridApi.selection.selectRow($scope.gridOptions.data[index]);
      });

    }

Non-working plunkr: http://plnkr.co/edit/XOliwXn2MLyH6nqO7pp4?p=preview

Can someone tell me why?

Community
  • 1
  • 1
user3255061
  • 1,757
  • 1
  • 30
  • 50

2 Answers2

2

I used below code for angular 1.5.0 version. It is working for me.

app.controller('MainCtrl', ['$scope', '$http', '$interval', 'uiGridConstants','$location', function ($scope, $http, $interval, uiGridConstants, $location) { ...............

// $interval whilst we wait for the grid to digest the data we just gave it

$interval( function() {$scope.gridApi.selection.selectRow($scope.gridOptions.data[0]);}, 0, 1);
Moon Mysterious
  • 164
  • 1
  • 14
0

It does work, you just need to wait for the grid to be loaded, for example with a small timeout. This way the browser renders your DOM and executes the JS afterwards, even with very small timeout. See this answer about timeout.

I updated your Plunkr.

setTimeout(function(){
  _.each($scope.gridOptions.data, function(companies, index){
    $scope.gridApi.selection.selectRow($scope.gridOptions.data[index]);
  });
}, 1);
Community
  • 1
  • 1
CMR
  • 933
  • 1
  • 7
  • 8
  • Thanks for your help, but according to https://github.com/angular-ui/ui-grid/issues/2267#issuecomment-128551701 there should be no more $timeout needed. – user3255061 Apr 07 '16 at 14:16