AngularJS + NG-Grid pass row.column.field into ng-click event

Woot4Moo picture Woot4Moo · Nov 6, 2013 · Viewed 19.8k times · Source

How does one pass {{row.getProperty(col.field)}} into ng-click? What happens is the id does not get propagated back, but the grid render correctly with the id.

code:

var app = angular.module('testing',['ngGrid']);
app.config(['$locationProvider', function($locationProvider)  
{  
       $locationProvider.html5Mode(true);
}]);    

app.controller('TestCtrl',function($scope)  
{  
      $scope.details = []; //whatever dummy data  
      $scope.loadById = function(id)  
      {  
            $window.location.href= 'newPage/?id='+id;
      };  

      $scope.gridOptions =  
      {  
           data: 'details',  
           columnDefs:[{field:'id',DisplayName:'id',  
                 cellTemplate:'<div class="ngCellText" ng-class="col.colIndex()"><a ng-click="loadById({{row.getProperty(col.field)}})">{{row.getProperty(col.field)}}</a></div>'  
              }]
      };    
});  

Answer

AardVark71 picture AardVark71 · Nov 7, 2013

You can just pass row in ng-click, instead of only the value outputted by the double brackets expression.

Your cellTemplate becomes this

cellTemplate:'<div class="ngCellText" ng-class="col.colIndex()"><a ng-click="loadById(row)">{{row.getProperty(col.field)}}</a></div>' 

Your function becomes this

$scope.loadById = function(row) {  
   window.console && console.log(row.entity);
   $window.location.href= 'newPage/?id='+ row.entity.id;
};