使用 CSSResources 设置 CellTable 中单个单元格的样式

发布于 2024-12-26 13:03:29 字数 2083 浏览 3 评论 0原文

基本上我想实现类似于 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; table = new CellTable(15,cellTableResources);

这就是我的自定义单元格的样子。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

空城旧梦 2025-01-02 13:03:29

“更优雅的方式”的问题在于,它意味着 CellTable 自己的样式将在其他地方有用,但它们可能不会。即使它们提供了样式的 getter,也会返回 Style 类型的实例,然后您必须将其转换为您自己的样式。

最好将此视为您的风格,它提供了一些选项:

  • 保留引用,以便您可以从单元
  • GWT 中访问它。在单元中创建客户端包的新副本并调用 EnsureInjected() - 它实际上只会注入一次,所以这确实不是问题,只是一个很好的做法,特别是如果有人决定使用您的单元格而不使用表格本身的样式。
  • 最后,将单元格所需的样式分解到它们自己的 clientbundle/cssresource 中,并使它们成为单元格本身的一部分。这使您可以完全打破单元格的依赖关系,甚至可以将其放入单元格表(而不是单元格列表或单元格浏览器等)中。

唯一棘手的部分是单元格上的样式是否依赖于表格中的样式,在这种情况下,您正在处理的这种令人讨厌的依赖关系是一件好事 - 它需要您意识到样式本身的依赖性。如果您选择第三个(我认为这是最干净的)选项,但仍然具有这种依赖性,您可以更进一步 - 在单元格中声明一个 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:

  • Keep a reference around so you can access it from within your cell
  • GWT.create a new copy of the client bundle within your cell and call ensureInjected() - it will only actually inject it once, so this really isn't a problem, just a good practice, esp if someone decides to use your cell without the style on the table itself.
  • And last, break out the styles needed for the cell into their own clientbundle/cssresource, and make them part of the cell itself. This lets you completely break apart the dependency of the cell on even being put in a celltable (as opposed to a celllist or cellbrowser, etc).

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文