Search code examples
angularjsangular-ui-gridui-grid

Binding onRegisterApi for gridOption within for loop in angularJS UI grid


I have a situation where I create gridOption with in for loop and also bind onRegisterApi for the same. But the loop variable in onRegisterApi method goes beyond loop variable. Code snippet will be like:

for(var k=0; k<3; k++){
 $scope.gridOptions[k] = {
      data : $scope.gridData[k],
      enableCellEditOnFocus : true,
      columnDefs : $scope.colmDef[k],
      onRegisterApi : function(gridApi){
           console.log(k) // here value of k is always 2.
           $scope.gridApi[k] = gridApi;
            console.log(k) // here value of k is always 3.
            $scope.gridApi[k].edit.on.afterCellEdit($scope, function(rowEntity, colDef, newValue, oldValue){
                    console.log(k) // here value of k is always 3.
                    /* I want to handle celledit of specific grid from gridApi. But this is not working as the variable 'k' doesn't change according to loop and it will be fixed always.*/
           })
      }

   }

}

So I want to assign onRegisterApi in loop for the gridOptions. Help me with this scenario please.


Solution

  • I could resolve this finally. I made changes as :

    for(var k=0; k<3; k++){
     $scope.gridOptions[k] = {
          data : $scope.gridData[k],
          enableCellEditOnFocus : true,
          columnDefs : $scope.colmDef[k],
          customStoredVar : k,
          onRegisterApi : function(gridApi){
               var gridRef = this;
               gridApi.edit.on.afterCellEdit($scope, function(rowEntity, colDef, newValue, oldValue){
                       var gridArrayRefVar = gridRef.customStoredVar; //this contains respective grid array value. So this value can be used for array ref
               })
          }
       }
    }