Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Primefaces open closable dialog programmatically

I want to dynamically create a primefaces dialog from the backing bean. I have written the code above:

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");
}

and in my jsf page: i have

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

 </h:form>

The problem is that when i set it to visible , i got the dialog but neither i can close (i don't get the close icon nor i can drag it)!

like image 269
Dhouha BOUSSALEM Avatar asked Jul 18 '14 09:07

Dhouha BOUSSALEM


1 Answers

You need to replace this line:

requestContext.openDialog("widget");

to that:

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

RequestContext.openDialog() method reffers to Primefaces Dialog Framework API which is different to p:dialog component.

From primefaces user guide:

Dialog Framework (DF) is used to open an external xhtml page in a dialog that is generated dynamically on runtime.

So, RequestContext.openDialog() expects you to provide path to xhtml page as argument.

And p:dialog component have javascript api show() and hide() methods to interact with it.

like image 182
alexSunder Avatar answered Oct 12 '22 09:10

alexSunder