如何设置GWT单元格背景颜色
问题描述:
我想更改gwt单元格列的背景颜色属性。问题是这种颜色可能会在单元格的每个渲染中发生变化(背景颜色取决于单元格的值)。如何设置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文件定义“动态”的颜色。
Thx for ur help!
答
如果您使用的是网格,则可以使用CellFormatter。例如。 grid.getCellFormatter()。setStyleName(row,column,“dynamicStyleName”);
对于'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 Designer中Refererence:
https://developers.google.com/web-toolkit/tools/gwtdesigner/tutorials/loginmanager
如何使用GWT设计实例
你使用哪种表格? FlexTable,网格? – aymeric 2012-08-16 18:30:54
这是关于CellTable的! – Fraternel 2012-08-17 09:55:33