2017-02-28 4 views
0

forループを使用してgridOptionを作成し、onRegisterApiをバインドする状況があります。しかし、onRegisterApiメソッドのループ変数はループ変数を超えています。コードスニペットは次のようになります。angularJS UIグリッドのforループ内でgridOptionに対してonRegisterApiをバインドします。

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.*/ 
      }) 
     } 

    } 

} 

したがって、gridOptionsのループでonRegisterApiを割り当てたいとします。このシナリオで私を助けてください。

答えて

0

私はこれを最後に解決できました。私は次のように変更しました:

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 
      }) 
     } 
    } 
} 
関連する問題