GWT: Delete Content of RootPanel(id)
I'm trying to delete the whole content of an RootPanel element based on an ID. The RootPanel returns correct and I can see it's content in the debugger. The problem is, that I delete it I tried the following things:
RootPanel rp = RootPanel.get("LayoutID2");
if (rp != null) {
for (Widget w开发者_如何学Pythonidget : rp) {
rp.remove(widget);
}
}
Any idea what I'm missing, or is there another function?
best regards, Stefan
All of the contents of the RootPanel
might not be widgets. For example if you placed following html in your host page:
<div id="LayoutID2">
Here goes the dynamic content
</div>
The text "Here goes the dynamic content" will not appear as a widget.
By the way, removal of all the widgets can be achieved by calling rp.clear()
.
The above answer is incorrect :D !
RootPanel.clear() removes widgets from the panel. I think you want to clear out existing HTML elements. RootPanel.get() automatically did this in 1.0.21, but this functionality was removed in 1.1. I use the following utility function to accomplish this:
public void clear(Element parent)
{
Element firstChild;
while((firstChild = DOM.getFirstChild(parent)) != null)
{
DOM.removeChild(parent, firstChild);
}
}
The accepted answer is correct in that all of the content in the RootPanel may not be widgets. It is also correct that rp.clear() will remove all of the widgets. However, if you want to do what the second answer suggests and completely clear the panel, there is a very easy approach that is not given here. Use:
rp.clear(true);
The optional boolean parameter will clear the DOM elements in the RootPanel as well as the Widgets. This is much simpler than the second answer.
精彩评论