我有一个ui-网格列定义如下:
$scope.gridOptions.columnDefs = [
{ name: 'Year 3', field: 'Year 3', cellEditableCondition: function($scope) { return $scope.row.entity[$scope.col['field']+'_editable']; },
editableCellTemplate: 'ui-grid/dropdownEditor', editDropdownValueLabel: 'yoy', editDropdownOptionsArray: $scope.yoyGrowthDropDown,
cellClass: function(grid, row, col, rowRenderIndex, colRenderIndex) {
if (row.entity[col['field']+'_editable'])
return 'red';
else
return "";
}
},
{ name: 'Year 4', field: 'Year 4', cellEditableCondition: function($scope) { return $scope.row.entity[$scope.col['field']+'_editable']; }, editableCellTemplate: 'ui-grid/dropdownEditor', editDropdownValueLabel: 'yoy', editDropdownOptionsArray: $scope.yoyGrowthDropDown,
cellClass: function(grid, row, col, rowRenderIndex, colRenderIndex) {
if (row.entity[col['field']+'_editable'])
return 'red';
else
return "";
}
},
{ name: 'Year 5', field: 'Year 5', cellEditableCondition: function($scope) { return $scope.row.entity[$scope.col['field']+'_editable']; }, editableCellTemplate: 'ui-grid/dropdownEditor', editDropdownValueLabel: 'yoy', editDropdownOptionsArray: $scope.yoyGrowthDropDown,
cellClass: function(grid, row, col, rowRenderIndex, colRenderIndex) {
if (row.entity[col['field']+'_editable'])
return 'red';
else
return "";
}
},
{ name: 'Year 6-10', field: 'Year 6-10', cellEditableCondition: function($scope) { return $scope.row.entity[$scope.col['field']+'_editable']; }, editableCellTemplate: 'ui-grid/dropdownEditor', editDropdownValueLabel: 'cagr', editDropdownOptionsArray: $scope.cagrDropDown,
cellClass: function(grid, row, col, rowRenderIndex, colRenderIndex) {
if (row.entity[col['field']+'_editable'])
return 'red';
else
return "";
}
} ];
对于每个cellEditableCondition & cellClass,都调用一个函数。我不想为每个列定义定义函数。相反,我想定义它一次,并为每一列调用它。
请有人告诉我,我们要定义函数的范围是什么,我们如何称呼它?
谢谢!
发布于 2017-05-24 11:38:07
您可以使用一个函数来完成它:
function isCellEditable(scope) {
return scope.row.entity[scope.col['field']+'_editable'];
}
function getCellClass(grid, row, col) {
return row.entity[col['field']+'_editable'] ? 'red' : '';
}
$scope.gridOptions.columnDefs = [
{ name: 'Year 3', field: 'Year 3', cellEditableCondition: isCellEditable,
editableCellTemplate: 'ui-grid/dropdownEditor', editDropdownValueLabel: 'yoy', editDropdownOptionsArray: $scope.yoyGrowthDropDown,
cellClass: getCellClass
},
{ name: 'Year 4', field: 'Year 4', cellEditableCondition: isCellEditable, editableCellTemplate: 'ui-grid/dropdownEditor', editDropdownValueLabel: 'yoy', editDropdownOptionsArray: $scope.yoyGrowthDropDown,
cellClass: getCellClass
},
{ name: 'Year 5', field: 'Year 5', cellEditableCondition: isCellEditable, editableCellTemplate: 'ui-grid/dropdownEditor', editDropdownValueLabel: 'yoy', editDropdownOptionsArray: $scope.yoyGrowthDropDown,
cellClass: getCellClass
},
{ name: 'Year 6-10', field: 'Year 6-10', cellEditableCondition: isCellEditable, editableCellTemplate: 'ui-grid/dropdownEditor', editDropdownValueLabel: 'cagr', editDropdownOptionsArray: $scope.cagrDropDown,
cellClass: getCellClass
}
];https://stackoverflow.com/questions/44151425
复制相似问题