我想更改 GWT 单元格列的背景颜色属性。问题是此颜色可能会在单元格的每次渲染时更改(背景颜色取决于单元格的值)。
我已经尝试覆盖TextColumn的单元格样式名称方法,如下所示:
@Override
public String getCellStyleNames(final Context context, final Object data) {
if (my_condition) return "a custom style";
else return "default style"; // or null...
}
正如您当然知道的那样,它只向属性添加一个类名,因此由于静态 css 文件定义,我无法使用它来"动态"设置颜色。
感谢您的帮助!
对于"color"属性的动态更新,我建议扩展TextCell(并将其传递给"TextColumn"构造函数)。像这样:
public class CustomCell extends TextCell<String> {
interface Template extends SafeHtmlTemplates {
@Template("<div style="color:{0}">{1}</div>")
SafeHtml div(String url, String text);
}
private static Template template;
public CustomCell () {
if (template == null) {
template = GWT.create(Template.class);
}
}
@Override
public void render(Context context, String value, SafeHtmlBuilder sb) {
String color = "red";
if (value != null) {
// The template will sanitize the URI.
sb.append(template.div(color, value));
}
}
}
public class CustomColumn<T> extends TextColumn<T> {
public CustomColumn() {
super(new CustomCell());
}
}
由于您没有提供正在使用的组件的详细信息,因此我将给出一个通用建议,以尝试找出您可能需要使用哪些属性。
我使用eclipse,并建议使用GWT设计器来帮助您处理POC问题。它帮助我了解我可能想要使用哪些属性:
-
GWT设计师推荐:https://developers.google.com/web-toolkit/tools/download-gwtdesigner
-
如何使用 GWT 设计器的示例:
https://developers.google.com/web-toolkit/tools/gwtdesigner/tutorials/loginmanager