2014-07-18 176 views
4

我想从后台bean动态创建一个primefaces对话框。 我写上面的代码:Primefaces以编程方式打开可关闭对话框

public void showDialog(){ 
    UIComponent panelGroup = facesContext.getViewRoot().findComponent("form1"); 
    System.out.println("found or not??"+ panelGroup.toString()); 
    Dialog dialog = new Dialog(); 
    dialog.setId("sample"); 
    dialog.setWidgetVar("widget"); 
    dialog.setHeader("Sample"); 
    dialog.setVisible(true); 
    dialog.setMinimizable(true); 

    dialog.setDynamic(true); 
    dialog.setHideEffect("fade"); 
    dialog.setFooter("footer"); 

    dialog.setDraggable(true); 
    dialog.setMinWidth(600); 
    dialog.setClosable(true); 
    dialog.setModal(true); 
    dialog.setAppendToBody(false); 

    panelGroup.getChildren().add(dialog); 

    RequestContext requestContext = RequestContext.getCurrentInstance(); 
    requestContext.openDialog("widget"); 
    requestContext.update("form1"); 
} 

,并在我的JSF页面:我有

<h:form id="form1" > 
     <h:commandButton value="show Dialog" action="#{createDialog.showDialog()}" /> 

</h:form> 

的问题是,当我把它设置为可见,我得到了对话,但无论是我可以关闭(我没有看到关闭图标,也不能拖动它)!

回答

3

需要更换这一行:

requestContext.openDialog("widget"); 

到:

requestContext.execute("PF('widget').show()");  

RequestContext.openDialog()方法reffers到Primefaces对话框框架API是至p不同:对话单元。

从primefaces的用户指南:

对话框框架(DF)被用于在被动态生成的运行时一个 对话框打开外部XHTML页面。

因此,RequestContext.openDialog()期望您提供作为参数的xhtml页面的路径。

而p:dialog组件有javascript api show()和hide()方法与它们进行交互。

+0

谢谢你的回答,它解决了问题,你帮我理解了这个问题。还有一件事我试图从后台添加内容到对话框中,包含html和jsf标签。怎么可能。 Thnx –

+0

您可以将jsf子组件添加到对话框中,就像添加对话框一样。链接解释如何添加html http://stackoverflow.com/questions/12301817/programmatically-created-html-components-in-jsf-managed-beans – alexSunder

相关问题