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?

2 Answers 2

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);
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for answering such an old question!
Wow, this really works. I tried without the interval and no rows were selected. With interval it worked perfect. Thanks ...
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);

1 Comment

Thanks for your help, but according to github.com/angular-ui/ui-grid/issues/… there should be no more $timeout needed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.