【问题标题】:primefaces commandbutton conditional statementprimefaces 命令按钮条件语句
【发布时间】:2023-03-20 17:59:02
【问题描述】:

我希望在我的命令按钮 (Primefaces 6.0) 中有一个条件语句,如果我的 java 方法返回 false 或 true,它应该显示一个对话框。类似的东西:

<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,如果可以删除则返回 true。

我的对话框如下所示:

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

(与“编辑”对话框相同的对话框)

【问题讨论】:

    标签: java primefaces conditional commandbutton


    【解决方案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 中阅读更多内容。

    希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-04
      • 1970-01-01
      • 1970-01-01
      • 2013-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多