javascript - 在 UI-Grid 中调用 cellClass 函数的通用函数

标签 javascript angularjs angularjs-directive angular-ui-grid

我打算为所有列调用相同的函数。我尝试了许多不同的方式来调用函数。没有一个起作用。我确定我做了一些愚蠢的事情。如果有人可以纠正我。

$scope.gridOptions = {
enableSorting: true,
columnDefs: [
  { field: 'name', 
    //cellClass:  howCanIhaveCommonFunction() 
  },
  { field: 'company',
    //cellClass:  howCanIhaveCommonFunction()

  }
]

};

 // below function this would be common for all columns
  var howCanIhaveCommonFunction = function(grid, row, col, rowRenderIndex, colRenderIndex) 
  {
          if (grid.getCellValue(row,col) === 'Velity' || grid.getCellValue(row,col) === 'Beryl Rice' ) {
            return 'blue';
          }
          return 'red';
  }

http://plnkr.co/edit/4yjVbhmfR47cf7FGfyFk?p=preview

最佳答案

是的,这是一个简单的错误,您没有只说函数名称,而是在没有任何参数的情况下调用它。

$scope.gridOptions = {
enableSorting: true,
columnDefs: [
  { field: 'name', 
    cellClass:  howCanIhaveCommonFunction 
  },
  { field: 'company',
    cellClass:  howCanIhaveCommonFunction

  }
]

您只需通过 () 使所有参数为空,只需传递函数名称,ui-grid 将使用正确的参数调用它。

关于javascript - 在 UI-Grid 中调用 cellClass 函数的通用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31308951/

相关文章:

javascript - Feathers JS 身份验证,无需电子邮件

javascript - 使用 select2 中选定的选项隐藏组件

javascript - 这个变量是一个数组。为什么它返回未定义?

javascript - 如何准备 grunt 来使用 app.js 准备 dist 文件夹?

javascript - AngularJS 方式中的 jQuery 函数

angularjs - 使用 Oauth2、Angular 和 yii2 REST 提供 protected 文件夹中的图像

javascript - Kendo UI Grid 在初始读取时未显示微调器/加载图标

javascript - Angular 渲染元素但不显示数据

windows - Windows 8 上带有 Express 的 node-webkit "Error: listen EACCES"

AngularJS - 根据单选按钮值有条件地应用所需的属性