你可能想看看这个SO Q&A。
的理念是:
- 您创建一个服务器调用(例如,RPC)来获取你的HTML模板为一个字符串。
- 您可以创建一个相应的HTMLPanel,它知道如何将动态元素附加到您在构造函数中传递的html模板中。
- 您可以将此新构建的HTMLPanel添加到您想要的页面根面板中。
On this SO你可以找到一个面板模板代码,可以帮助你。其中的版本没有“动态”html源代码,而是在ClientBundle中进行了硬编码。但它很容易扩展,因此HTML源文件是动态的,只需添加一个construtor,如:
public HtmlPanelBase(final String htmlContentAsText)
其中htmlContentAsText
是模板HTML字符串从您的服务器如下:
public class HtmlPanelBase extends Composite
{
private String _dynPostfix = "";
protected final String id(final String staticId) { return staticId + _dynPostfix; }
private final String wrapId(final String id) { return "id=\"" + id + "\""; }
private final String wrapDynId(final String refId) { return wrapId(id(refId)); }
private String _htmlContentAsText = null;
protected String getHtmlContentAsText() { return _htmlContentAsText; }
private ArrayList<String> _idList = null;
protected HTMLPanel _holder = null;
private HTMLPanel createHtmlPanel(final boolean defineGloballyUniqueIds)
{
// HTML panel text containing the reference id's.
if (defineGloballyUniqueIds)
{
// Replace the reference id's with dynamic/unique id's.
for (String refId : _idList)
_htmlContentAsText = _htmlContentAsText.replace(wrapId(refId), wrapDynId(refId));
}
// Return the HTMLPanel containing the globally unique id's.
return new HTMLPanel(_htmlContentAsText);
}
public HtmlPanelBase(final String htmlContentAsText, final ArrayList<String> idList, final boolean defineGloballyUniqueIds)
{
_htmlContentAsText = htmlContentAsText;
_idList = idList;
this.setup(defineGloballyUniqueIds);
super.initWidget(_holder);
}
public HtmlPanelBase(final String htmlContentAsText)
{
this(htmlContentAsText, null, false);
}
private void setup(final boolean defineGloballyUniqueIds)
{
if (defineGloballyUniqueIds)
_dynPostfix = "_" + UUID.uuid().replace("-", "_");
_holder = createHtmlPanel(defineGloballyUniqueIds);
}
}
要使用,取你的htmlContentAsText在服务器上(可能取决于区域设置),在成功实例化上述基本模板的派生类时,在构造函数中传递提取的htmlContentAsText,并在其中添加所有的逻辑修改或添加基本html - 例如,添加处理程序对用户操作的响应。
当然,请分享代码。 –
@ClickUpvote完成,添加了示例代码。 – Patrick
你能解释一下这段代码与使用普通的HTMLPanel不同吗?另外,我没有看到改变这个面板的html代码的方法,你能澄清一下吗? –