0

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>
juanpalomo
  • 63
  • 1
  • 8
  • 1
    The confirm dialog is for the direct client-side usage. If you want to have a server side initiated 'confirm', then use a normal dialog. Just do not submit anything when clicking the first button and do when you click the button in the normal dialog that is opened from the server. Not sure what the advantage is though... – Kukeltje Nov 02 '18 at 15:38

1 Answers1

-1

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();")
}
Oscar Mera
  • 64
  • 1
  • 7
  • The method execute(String) from the type RequestContext is deprecated, should I use PrimeFaces current = PrimeFaces.current(); current.executeScript("PF('confirmDialogwidget').show();"); – juanpalomo Nov 02 '18 at 15:04
  • What version of primefaces do you use? I use the 6.1 in that version is not deprecated. – Oscar Mera Nov 02 '18 at 15:08
  • `PrimeFaces current = PrimeFaces.current(); current.executeScript("PF('confirmDialogwidget').show();");` solve your problem? – Oscar Mera Nov 02 '18 at 15:15
  • but you need to be able to call the confirmDialog from the bean? – Oscar Mera Nov 02 '18 at 15:45
  • yes I make a existById query and if its true i want to ask the user if he wants to make an update in database – juanpalomo Nov 02 '18 at 15:51
  • Okay, I'm glad to help. Don't forget to mark the question as resolved. – Oscar Mera Nov 02 '18 at 16:17
  • Downvoted this answer as it is as it stands NOT a solution. And even with the suggestion to just change to a normal `p:dialog` (like earlier in my comment) is **not sufficient** – Kukeltje Nov 03 '18 at 08:39