jsfprimefacesconfirm-dialog

How can I show p:confirmDialog from bean?


I got this from primefaces but I dont want to use the button and call the confirm dialog from the bean, how can I do this or render commandbutton and execute from bean?

The problem is that Im using a commandbutton to execute a method, this method has an if in it when true then I want to show a confirm dialog.

<p:commandButton value="Destroy the World" actionListener="#{dialogView.destroyWorld}" update="message">
    <p:confirm header="Confirmation" message="Are you sure?" icon="ui-icon-alert" />
</p:commandButton>

<p:confirmDialog global="true" showEffect="fade" hideEffect="fade">
    <p:commandButton value="Yes" type="button" styleClass="ui-confirmdialog-yes" icon="ui-icon-check" />
    <p:commandButton value="No" type="button" styleClass="ui-confirmdialog-no" icon="ui-icon-close" />
</p:confirmDialog>

Solution

  • you can do the following

     <p:commandButton value="Destroy the World" onclick="PF('confirmDialogwidget').show();">
     </p:commandButton>
    
    
    
    <p:confirmDialog global="true" showEffect="fade" hideEffect="fade" header="Are you sure?"
                         widgetVar="confirmDialogwidget">
        <p:commandButton value="Yes" type="button" styleClass="ui-confirmdialog-yes" icon="ui-icon-check" />
        <p:commandButton value="No" type="button" styleClass="ui-confirmdialog-no" icon="ui-icon-close" 
                         onclick="PF('confirmDialogwidget').hide();" />
    </p:confirmDialog>
    

    or also using bean

    <p:commandButton value="Destroy the World" actionListener="#{dialogView.destroyWorld}">
    </p:commandButton>
    
    
    <p:confirmDialog global="true" showEffect="fade" hideEffect="fade" header="Are you sure?"
                     widgetVar="confirmDialogwidget">
        <p:commandButton value="Yes" type="button" styleClass="ui-confirmdialog-yes" icon="ui-icon-check" />
        <p:commandButton value="No" type="button" styleClass="ui-confirmdialog-no" icon="ui-icon-close" 
                         onclick="PF('confirmDialogwidget').hide();"/>
    </p:confirmDialog>
    

    Here it opens from the bean

     public void destroyWorld(){
        RequestContext.getCurrentInstance().execute("PF('confirmDialogwidget').show();")
    }