0

我正在尝试使用在 UI Binder 模板中定义的 a 来初始化 aTreeItem作为WidgetTreeItem内容。最初我从这样的模板开始:

<g:Tree>
    <g:TreeItem>
        <g:HTMLPanel>
            <p>This is the root item's content.</p>
        </g:HTMLPanel>
        <g:TreeItem>
            <g:HTMLPanel>
                <p>This is the child's content.</p>
            </g:HTMLPanel>
        </g:TreeItem>
    </g:TreeItem>
</g:Tree>

期望最终得到一个像这样的树结构:

+ This is the root item's content.

扩展为:

- This is the root item's content.
 \ This is the child's content.

但这不起作用。这些HTMLPanel小部件是作为TreeItems 的子级创建的,而不是作为项目的小部件,所以我最终得到了这样的结构:

- (root item has no content)
 \ This is the root item's content.
  - (first child has no content)
   \ This is the child's content.

所以我尝试像这样扩展TreeItem类:

public class WidgetTreeItem extends TreeItem {

    @UiChild(tagname="widget", limit=1)
    public void setWidget(IsWidget isWidget) {
        super.setWidget(isWidget.asWidget());
    }

}

然后,将包含的包WidgetTreeItem导入c命名空间,我有一个这样的模板:

<g:Tree>
    <c:WidgetTreeItem>
        <c:widget>
            <g:HTMLPanel>
                <p>This is the root item's content.</p>
            </g:HTMLPanel>
        </c:widget>
        <!-- Should create a child TreeItem with the HTMLPanel as its Widget. -->
        <g:HTMLPanel>
            <p>This is the child's content.</p>
        </g:HTMLPanel>
    </c:WidgetTreeItem>
</g:Tree>

但这也行不通。它失败,但有以下异常:

Only TreeItem or Widget subclasses are valid children: <c:WidgetTreeItem>.

谁能看到如何实现我正在尝试的目标?

4

2 回答 2

0

从这个答案中得到一点启发,我找到了我的解决方案。给定我原来的例子,XML 现在看起来像这样:

<g:Tree>
    <g:HTMLPanel ui:field="rootContent">
        <p>This is the root item's content.</p>
    </g:HTMLPanel>
    <g:HTMLPanel ui:field="childContent">
        <p>This is the child's content.</p>
    </g:HTMLPanel>
    <g:TreeItem widget="{rootContent}">
        <g:TreeItem widget="{childContent}">
        </g:TreeItem>
    </g:TreeItem>
</g:Tree>

这里需要一些玩耍。最值得注意的是,这仅在内容面板包含在实现HasWidgets界面的小部件中时才有效。该类Tree满足此要求。还值得注意的是,内容字段必须在使用之前定义。

于 2013-09-10T13:36:58.817 回答
0

可能以下可以帮助你。

<g:Tree>
    <g:TreeItem text="This is the first parent item's content.">
        <g:TreeItem text="This is the child 1's content." />
        <g:TreeItem text="This is the child 2's content." />

    </g:TreeItem>

    <g:TreeItem text="This is the second parent item's content.">
        <g:TreeItem text="This is the child 3's content." />
        <g:TreeItem text="This is the child 4's content." />

    </g:TreeItem>
</g:Tree>

您可以根据需要附加 TreeItems。

于 2013-09-10T13:43:21.797 回答