我正在使用 Ext-GWT,我认为 ListView
是适合我需要的布局。我的问题是我必须为所有项目使用 HTML 模板,但我想构建 GWT/Ext-GWT 小部件,所以我使用 div
我将用适当的小部件替换占位符。
如何更换我的 div
用小部件?我的第一次尝试是使用 RootPanel.get('div-id')
,但显然你无法创建 RootPanel
它位于小部件中(我使用 Debug模式单步执行代码,直到发现静默异常)。
public class TicketContainer extends LayoutContainer {
private ArrayList<BasicTicket> tickets;
public TicketContainer(ArrayList<BasicTicket> tickets) {
this.tickets = tickets;
}
@Override
protected void onRender(Element parent, int index) {
super.onRender(parent, index);
setLayout(new FlowLayout(1));
ListStore<BasicTicket> store = new ListStore<BasicTicket>();
store.add(this.tickets);
ListView<BasicTicket> view = new ListView<BasicTicket>(store);
view.addListener(Events.Refresh, new Listener<BaseEvent>() {
@Override
public void handleEvent(BaseEvent be) {
for (BasicTicket ticket : tickets) {
// At this point I need to find the div with the id
// "ticket_"+ticket.getId() and replace it with a GWT
// widget that I can add events to and enable drag and drop
}
}
});
add(view);
}
private native String getTemplate() /*-{
return ['<tpl for=".">',
'<div id="ticket_{id}"></div>',
'</tpl>'].join("");
}-*/;
}
完整来源位于https://code.launchpad.net/~asa-ayers/+junk/Kanban如果您需要代码中的其他上下文。
最佳答案
在“纯”GWT 中,答案是使用 HTMLPanel
:
String id = DOM.createUniqueId();
HTMLPanel panel = new HTMLPanel("<div class=\"content\" id=\"" + id + "\"></div>");
panel.add(new Label("Something cool"), id);
如您所见,com.google.gwt.user.client.ui.HTMLPanel.add(Widget, String)
采用 HTMLPanel
中元素的 ID code> 并将 Widget
放置在该元素内。
我没有使用过 Ext-GWT,但您可以使用 HTMLPanel
或在 Ext-GWT 中搜索等效项。
关于java - 当 div 已包含在另一个小部件中时,如何用小部件替换它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1514611/