从Grid定义调用函数

时间:2017-05-24 07:13:47

标签: angularjs angular-ui-grid ui-grid

我有一个ui-grid列定义如下:

$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调用一个函数。 我不想为每个列定义定义函数。 相反,我想定义一次并为每一列调用它。

有人可以告诉我们我们在什么范围内定义功能以及如何调用它?

谢谢!

1 个答案:

答案 0 :(得分:0)

您可以使用以下功能执行此操作:

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
  } 
  ];