简体   繁体   中英

primefaces commandbutton conditional statement

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)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM