首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从网格定义调用函数

从网格定义调用函数
EN

Stack Overflow用户
提问于 2017-05-24 07:13:47
回答 1查看 967关注 0票数 0

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

代码语言:javascript
复制
$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,都调用一个函数。我不想为每个列定义定义函数。相反,我想定义它一次,并为每一列调用它。

请有人告诉我,我们要定义函数的范围是什么,我们如何称呼它?

谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-05-24 11:38:07

您可以使用一个函数来完成它:

代码语言:javascript
复制
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
  } 
  ];
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44151425

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档