Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular ngGrid select row on page load

My question is an extension to thisquestion

Getting select rows from ng-grid?

plunker - http://plnkr.co/edit/DiDitL?p=preview

I need a row to be selected on page load and I need to avoid listening to 'ngGridEventData'

calling $scope.gridOptions.selectRow(2, true); in the controller body fails since the grid has not been loaded.

avoiding listening to ngGridEventData is due to the fact that I need the controller to listen to an event triggered before and based on that I need to select the nggrid row.

any ideas?

like image 853
Sanath Avatar asked Aug 29 '13 07:08

Sanath


2 Answers

Add $timeout to your Controller and do this:

$timeout(function() { 
    $scope.gridOptions.selectRow(2, true); 
});

Example: http://plnkr.co/edit/hDv7b8?p=preview

like image 193
user508994 Avatar answered Oct 12 '22 11:10

user508994


A solution without timeout can be found here: https://github.com/angular-ui/ui-grid/issues/2267 and here: Pre-Select rows on load with angular-ui-grid

$scope.gridOptions = {
...
                onRegisterApi : function (gridApi) {
                    $scope.gridApi = gridApi;
                    $scope.gridApi.grid.modifyRows($scope.gridOptions.data);
                    $scope.gridApi.selection.selectRow($scope.gridOptions.data[0]);
                }
            };

Apparently modifyRows requires v3.0.0-rc.22+

like image 40
rob2universe Avatar answered Oct 12 '22 11:10

rob2universe