使用UiBinder生成单元格

Nic*_*ner 3 gwt uibinder

GWT提供以下作为构建单元格的示例CellList:

  /**
   * A simple data type that represents a contact.
   */
  private static class Contact {
    private static int nextId = 0;

    private final int id;
    private String name;

    public Contact(String name) {
      nextId++;
      this.id = nextId;
      this.name = name;
    }
  }

  /**
   * A custom {@link Cell} used to render a {@link Contact}.
   */
  private static class ContactCell extends AbstractCell<Contact> {
    @Override
    public void render(Context context, Contact value, SafeHtmlBuilder sb) {
      if (value != null) {
        sb.appendEscaped(value.name);
      }
    }
  }
Run Code Online (Sandbox Code Playgroud)

如果我有一个复杂的单元格,只需返回一个安全的HTML字符串render()变得乏味.有没有办法使用UiBinder,或者比手工构建HTML字符串更好的方法?

Tho*_*yer 5

GWT 2.5将UiRenderer完全为此目的添加:利用*.ui.xml模板构建渲染器,使用模板变量,在渲染时获取子元素的句柄等等.

等待GWT 2.5,您可以使用SafeHtmlTemplates,将模板拆分为单独的方法,然后将它们合成以构建单元格的内容.