Calling Primefaces dialog box from Managed Bean function

103,788

Solution 1

You can, by using the RequestContext (or PrimeFaces if you are using the version 6.2 or higher) class.

Suppose you have the following:

<p:dialog id="myDialogID" widgetVar="myDialogVar">
....
</p:dialog>

So the way you do in the facelet itself, i.e. onclick=myDialogVar.show();, the same can be done in your managed bean like so:

For PrimeFaces <= 3.x

RequestContext context = RequestContext.getCurrentInstance();
context.execute("myDialogVar.show();");

For PrimeFaces >= 4.x to PrimeFaces < 6.2 (as per @dognose and @Sujan)

RequestContext context = RequestContext.getCurrentInstance();
context.execute("PF('myDialogVar').show();");

For PrimeFaces >= 6.2

PrimeFaces current = PrimeFaces.current();
current.executeScript("PF('myDialogVar').show();");

This is for using targeted dialogs. If you just need to show a message without giving preference to any custom dialog, then you can do it this way:

FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Message Title", "Message body");

// For PrimeFaces < 6.2
RequestContext.getCurrentInstance().showMessageInDialog(message);

// For PrimeFaces >= 6.2
PrimeFaces.dialog().showMessageDynamic(message);

You can pass in arguments and set callbacks as well. Refer to the showcase examples in the link.

See also:

Solution 2

If you are on primeface 4.0 Or Above:

RequestContext.getCurrentInstance().execute("PF('yourdialogid').show()");

Solution 3

Vrushank's solution is correct.

There is also another way: in your dialog, bind rendered attribute to a boolean value of your bean, and set the visible attribute to true, like this:

<p:confirmDialog
        widgetVar="myDialog"
        visible="true"
        rendered="#{myBean.showMyDialog}">

In action listener in your bean, you just call setShowMyDialog(true), and a dialog will show (assuming your dialog is being updated by this action). This approach is useful if for some reason you don't want your dialog's HTML to be rendered when it is not visible to the user. This way, in your bean you have access to information, whether your dialog is visible.

Another benefit of this solution is, that your dialog is not hidden on ajax update (of dialog or its parent).

Share:
103,788
NKS
Author by

NKS

Updated on July 31, 2020

Comments

  • NKS
    NKS almost 4 years

    Hi I have a managed bean with some functions , based on some condition in that function I will like to call a dialog box

    Managed bean function goes as

    public String editStudent(){    
        setReadOnly(false);     
        setButton(true, true, true, false, true, true,true);
        LockItem lItem;
        if(selectStudent !=null){
            lItem = (LockItem) services.getbyId("LockItem", condition);
            if (lItem == null){
                System.out.println("Student Avalibale for process :::"); 
                studentRedirect();
                return "studentEdit.jsf?faces-redirect=true";
            } else {
                 //To show dialog from here
                 System.out.println("Student Not Avalibale : Locked By " + lItem.getLockedBy());
            }
        } else {
            FacesMessage msg;
            msg = new FacesMessage("Please select the record.");
            FacesContext.getCurrentInstance().addMessage(null, msg);
            return show("menu");
        }
    }
    

    Is there any method using which we can call the dialog box from such managed function