简体   繁体   English

如何在Dojo数据网格中有条件地设置单元格样式?

[英]How do you conditionally style a cell in a Dojo data grid?

Essentially what I want to do is to apply additional CSS classes to individual cells in a data grid based on the value of the cell. 基本上我想要做的是根据单元格的值将额外的CSS类应用于数据网格中的单个单元格。 An example would be to color the text red when a dollar value is negative. 一个例子是当美元值为负时将文本着色为红色。

The only solution I've found was to use the formatter of the column to create a string for a span that has the class based on the value passed in. I figure there has to be a better way. 我发现的唯一解决方案是使用列的格式化程序为跨度创建一个字符串,该字符串具有基于传入的值的类。我认为必须有更好的方法。

When specifying the structure, you pass in an object that represents the widget configuration for a given column. 指定结构时,将传入一个对象,该对象表示给定列的窗口小部件配置。 As part of this object, include a formatter function in the definition: 作为此对象的一部分,在定义中包含formatter函数:

{
...
 formatter: function(val, rowIdx, cell) {
    classes = compute_classes(val, rowIdx, cell);
    cell.customClasses.push(classes);
  }
}

however your 'compute_classes' computes the classes to use is up to you. 但是你的'compute_classes'计算要使用的类取决于你。 They will be applied to the cell, and then you can manage their appearance in your CSS. 它们将应用于单元格,然后您可以在CSS中管理它们的外观。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM