我有一种情况,我在for循环中创建gridOption,并且同样绑定onRegisterApi。但是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 in循环。请帮我解决这个问题。
答案 0 :(得分: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
})
}
}
}