2017-02-08 44 views
1

我想要一個條件語句在我的commandbutton(Primefaces 6.0)中,如果我的java方法返回false或true,它應該顯示一個對話框。類似的東西:primefaces commandbutton條件語句

<p:commandButton onclick="(myJavaMethod) ? deleteDialog.show() : confirmDialog.show()"> 
 
    <p:confirm header="Deleting Branch" message="Do you want to delete the Branch?"/> 
 
</p:commandButton>

myJavaMethod返回false,如果我不能刪除它和真正的,如果我可以將其刪除。

我的對話框如下圖所示:

<!-- 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>

(同一個對話框與「編輯」對話框)

回答

1

你正在嘗試做的是調用服務器端方法有onclick,你必須知道onclick 只是一個客戶端方法,你可以用它來調用一個javascript方法,而javascript方法將調用一個p:remoteCommand 這是一個簡單的例子,但我很確定你可以在其他帖子中找到更多關於這個主題的文章閱讀這篇文章希望 會給出更多關於它的信息How to call JSF backing bean method only when onclick event occurs

關於你的問題,你可以用你的方法來有條件地調用你的對話框

讓我們看看這個例子:

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();");  
}  
...  
} 

,並在您的XHTML頁面會看起來像這樣

myXHTMLpage.xhtml

<p:commandButton actionListener="#{managedBean.myJavaMethod}"> 
... 
</p:commandButton> 

你可以閱讀更多的在這個崗位Calling Primefaces dialog box from Managed Bean function

希望能幫到你。

+0

它解決你的問題嗎?!? –