问题
I want a conditional statement in my commandbutton (Primefaces 6.0) which should show a dialog if my java method return false or true. Something like that:
<p:commandButton onclick="(myJavaMethod) ? deleteDialog.show() : confirmDialog.show()">
<p:confirm header="Deleting Branch" message="Do you want to delete the Branch?"/>
</p:commandButton>
myJavaMethod return false if i can't delete it and true if i can delete it.
My Dialogs look like this:
<!-- DELETE-DIALOG -->
<p:dialog id="deleteDialog" widgetVar="deleteDialog">
<h:form id="deleteDialogForm">
<h:panelGrid columns="1" border="0">
<p:outputLabel value="Branch could not be deleted"/>
<p:commandButton icon="ui-icon-close" id="doCloseDialog" oncomplete="PF('deleteDialog').hide()" value="OK" class="btn-confirm"/>
</h:panelGrid>
</h:form>
</p:dialog>
(Same Dialog with 'Edit' Dialog)
回答1:
What you are trying to do is to call a server side method with onclick
, and you have to know that onclick
is only a client side method, you can use it to call a javascript method and the javascript method will call a p:remoteCommand
this is a simple example but i am pretty sure you can found more in other post to start about this topic read this post hope that will
give more informations about it How to call JSF backing bean method only when onclick event occurs .
about your question you can use your method to conditionally call your dialog
Let see this example :
ManagedBean.java
public void myJavaMethod () {
...
if( condition ){
RequestContext context = RequestContext.getCurrentInstance();
context.execute("PF('myDialogVar').show();");
} else {
RequestContext context = RequestContext.getCurrentInstance();
context.execute("PF('myDialogVarOther').show();");
}
...
}
and in your xhtml page it will look like this
myXHTMLpage.xhtml
<p:commandButton actionListener="#{managedBean.myJavaMethod}">
...
</p:commandButton>
you can read more in this post Calling Primefaces dialog box from Managed Bean function.
Hope that helped you.
来源:https://stackoverflow.com/questions/42107052/primefaces-commandbutton-conditional-statement