使用 CSSResources 设置 CellTable 中单个单元格的样式
基本上我想实现类似于 GWT 文档
但是,我不想直接在 DIV 元素上指定样式,而是想从我为我定义的自定义 CSSResource
分配一个混淆的样式名称单元格表。
下面是一些代码:
我为我的 CellTable 定义了一个自定义 Resources
接口:
public interface CellTableResources extends Resources {
@Source({CellTable.Style.DEFAULT_CSS,CellTableStyle.STYLE})
CellTableStyle cellTableStyle();
public interface CellTableStyle extends Style {
String STYLE = "CellTable.css";
public Sring coloredCell();
}
}
我将其传递给我的 CellTable 的构造函数:
CellTable
这就是我的自定义单元格的样子。
public class ColorCell extends AbstractCell<String> {
interface Templates extends SafeHtmlTemplates {
@SafeHtmlTemplates.Template("<div class=\"{0}\">{1}</div>")
SafeHtml cell(String classname, SafeHtml value);
}
private static Templates templates = GWT.create(Templates.class);
@Override
public void render(Context context, String value, SafeHtmlBuilder sb) {
if (value == null) {
return;
}
// how can I access the CSSResources which I pass to the CellTable
CellTableResources ressources = ?
String className = ressources.cellTableStyle().coloredCell();
SafeHtml safeValue = SafeHtmlUtils.fromString(value);
SafeHtml rendered = templates.cell(className, safeValue);
sb.append(rendered);
}
}
如何访问我传递到自定义单元格中的 CellTable 的 CellTableRessources
? 这是重要的部分:
// how can I access the CSSResources which I pass to the CellTable
CellTableResources ressources = ?
String className = ressources.cellTableStyle().coloredCell();
我提出的唯一解决方案是将 CellTableRessources
传递给我的 AbstractCell
的构造函数。 是否有更优雅的方法(我已经将其传递给 CellTable)。
我认为主要问题是:
“如何从单元格或列访问 CellTable 变量?”
Basically I want to implement something similar to the the cell-coloring which is defined in the GWT documentation
However I don't want to specify the style directly on the DIV element but want to assign an obfuscated stylename from my custom CSSResource
which I defined for my CellTable.
Here is some code:
I defined a custom Resources
interface for my CellTable:
public interface CellTableResources extends Resources {
@Source({CellTable.Style.DEFAULT_CSS,CellTableStyle.STYLE})
CellTableStyle cellTableStyle();
public interface CellTableStyle extends Style {
String STYLE = "CellTable.css";
public Sring coloredCell();
}
}
I pass it to the constructor of my CellTable:
CellTable<XY> table = new CellTable<XY>(15,cellTableResources);
This is how my custom cell looks like.
public class ColorCell extends AbstractCell<String> {
interface Templates extends SafeHtmlTemplates {
@SafeHtmlTemplates.Template("<div class=\"{0}\">{1}</div>")
SafeHtml cell(String classname, SafeHtml value);
}
private static Templates templates = GWT.create(Templates.class);
@Override
public void render(Context context, String value, SafeHtmlBuilder sb) {
if (value == null) {
return;
}
// how can I access the CSSResources which I pass to the CellTable
CellTableResources ressources = ?
String className = ressources.cellTableStyle().coloredCell();
SafeHtml safeValue = SafeHtmlUtils.fromString(value);
SafeHtml rendered = templates.cell(className, safeValue);
sb.append(rendered);
}
}
How can I access my CellTableRessources
that I passed to my CellTable in my custom cell?
Here is the important part:
// how can I access the CSSResources which I pass to the CellTable
CellTableResources ressources = ?
String className = ressources.cellTableStyle().coloredCell();
The only solution I come up with is to pass the CellTableRessources
to the constructor of my AbstractCell
.
Isn't there a more elegant way (I already have passed it to the CellTable).
I think the main question is:
"How can I access CellTable variables from a Cell or Column?"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
“更优雅的方式”的问题在于,它意味着 CellTable 自己的样式将在其他地方有用,但它们可能不会。即使它们提供了样式的 getter,也会返回 Style 类型的实例,然后您必须将其转换为您自己的样式。
最好将此视为您的风格,它提供了一些选项:
唯一棘手的部分是单元格上的样式是否依赖于表格中的样式,在这种情况下,您正在处理的这种令人讨厌的依赖关系是一件好事 - 它需要您意识到样式本身的依赖性。如果您选择第三个(我认为这是最干净的)选项,但仍然具有这种依赖性,您可以更进一步 - 在单元格中声明一个 style/clientbundle,但像对 CellTable 的 ClientBundle 所做的那样扩展它 - 并且由于这些是接口,因此您可以创建一个扩展这两个接口的包,并将其提供给每个表和单元格。
The problem with a 'more elegant way' is that it implies that CellTable's own styles will be useful elsewhere, which they probably won't. Even if they provided a getter for style, that would return an instance of type Style, which you would then have to cast to your own style.
It is best to consider this to be your style, which presents a few options:
The only tricky part is if the styles on the cell do depend on the styles in the table, in which case this annoying dependency you are dealing with is a good thing - it is requiring you to be aware of that dependency in the styles themselves. If you go for the third (I see this as the cleanest) option but still have this dependency, you can go a step further - declare a style/clientbundle in your cell, but extend it like you are doing to the CellTable's ClientBundle - and since these are interfaces, you can make one bundle that extends both of these, and is supplied to each table and cell.