mrjimoy_05 mrjimoy_05 - 2 months ago 15
Java Question

Primefaces call ConfirmDialog from Backing

I would like to call

confirmDialog
via backing. This code works perfectly, but how can I set the message and set the actionlistener of the confirmDialog via backing? There is two condition, while:


  • The user check the option A on the checkbox (I omitted the code), then it should be directly print a text to console. --> This one is done by the code below

  • The user check the option B on the checkbox, then it should be show the confirmDialog and while the user press YES button, it should be call another function on the backing.



How to do that? Thanks.

<p:commandButton value="Execute" icon="ui-icon-circle-check" update="frmContent" actionListener="#{backing.validate}" />

<p:confirmDialog id="cfmDlg" widgetVar="wvCfmDlg" global="true" >
<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>


In Backing:

public void validate() {
if(mode.equals("1")) {
System.out.println("OK");
} else {
//call confirmDialog and set message + action listener
RequestContext context = RequestContext.getCurrentInstance();
context.execute("wvCfmDlg.show();");
}
}

Answer

If I understood your question correctly.. I would do it in this way.

xhtml

<p:commandButton style="display: none" 
                 widgetVar="confirmButton"  
                 actionListener="#{backing.yesFunction}" >
   <p:confirm header="Confirmation" message="Are you sure?" /> 
</p:commandButton>

<p:commandButton value="Execute"
                 actionListener="#{backing.validate}" /> 

<p:confirmDialog id="cfmDlg" global="true" >
      <p:commandButton value="Yes" />
      <p:commandButton value="No" />
</p:confirmDialog>

bean

public void validate() {
   if(mode.equals("1")) {
       System.out.println("OK");
   } else {
    RequestContext context = RequestContext.getCurrentInstance();
    context.execute("PF('confirmButton').jq.click();");
   }
}

Basically you add a hidden button (with p:confirm) in the usual way and you click it through jQuery.