Search code examples
angularjsangular-ui-grid

Set focus to newly added row in ui-grid


I am using ui-grid. The use-case is such that on clicking a button, a new row is added. I want to set focus on the first cell of the newly added row Any suggestions on how to do it ? Here's a plunkr for the same. http://plnkr.co/edit/6zyZ5q

$scope.addNew = function() {
var newobj = {"id": "","name":"" }; 
$scope.gridOptions.data.unshift(newobj);
$scope.gridApi.core.notifyDataChange(uiGridConstants.dataChange.ROW); 

//set focus to the new row 
$scope.gridApi.cellNav.scrollToFocus( $scope.gridOptions.data[0], $scope.gridOptions.columnDefs[0]);
};

Solution

  • $timeout(function() {
      $scope.gridApi.cellNav.scrollToFocus(
        $scope.termGridOptions.data[0],
        $scope.termGridOptions.columnDefs[0]
     );
    });
    

    I hope it helps.