如何根据条件执行(或不执行)ajax调用 [英] how to execute (or not) an ajax call based on a condition

查看:45
本文介绍了如何根据条件执行(或不执行)ajax调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用Ajax执行调用,但是首先,我还必须对后备bean的方法进行验证.基于此方法返回的布尔值,ajax调用将被执行(返回true)还是不执行(例如返回false).

I need to execute a call using Ajax, but first, I also have to make a validation on my backing bean's method. Based on a boolean returned by this method, the ajax call will be executed (returned true) or not (returned false, for example).

为更具体起见,并提供一个易于理解的示例,我试图在单击按钮后打开一个对话框.

To be more specific and to give a touchable example, I am trying to open a dialog after I click on a button.

这是我的.xhtml代码:

This is my .xhtml code:

<h:form id="tableVendasNaoPagas">
    <p:dataTable value="#{relatorioVendaMB.vendasNaoPagas}" var="venda" rowKey="#{venda.codigo}" emptyMessage="Nenhuma venda encontrada" selection="#{relatorioVendaMB.vendasAPagar}">
    <f:facet name="header">Tabela de Venda</f:facet>
    <p:column selectionMode="multiple" style="width:16px;text-align:center"/>
    <p:column headerText="Código" sortBy="#{venda.codigo}">
        <h:outputText value="#{venda.codigo}"/>
    </p:column>
    <!--Other columns-->
    <f:facet name="footer">
        <p:commandButton id="btnAbreDialogEfetuarPagamento" value="Efetuar Pagamento" process="@form" update=":formConfirmarPagamento, :formAux:growlglobal, @this" oncomplete="if (#{relatorioVendaMB.abreDialogEfetuaPag()}) { PF('dialogConfirmarPagamento').show() }"/>
    </f:facet>
    </p:dataTable>
</h:form>
.
.
.
<h:form id="formConfirmarPagamento">
    <p:dialog id="dialogConfirmarPagamento" ...>
</h:form>

.java:

public boolean abreDialogEfetuaPag () {
    if (vendasAPagar == null || vendasAPagar.isEmpty()){
        MensagensUtil.mensagemAviso("Atenção", "selecione ao menos uma venda");
        return false;
    } else {
        return true;
    }
}

仅当从表中选择了至少一项(对话框 vendasAPagar 不为空)时,才应显示该对话框.
不需要在支持bean上调用该方法,但这很方便,因为我可以向< p:growl> 添加一条消息并向用户提供反馈(例如此处: MensagensUtil.mensagemAviso(Atenção","selecione ao menos uma venda"); )

The dialog should be displayed only if at least one item from the table has been selected (the list vendasAPagar is not empty).
The call to the method on the backing bean isn't required, but it would be convenient, because I could add a message to a <p:growl> and give a feedback to the user (Like here: MensagensUtil.mensagemAviso("Atenção", "selecione ao menos uma venda");)

我尝试了许多不同的方法,但是它们也不起作用:

I've tried many different approaches, but they didn't work either:

<p:commandButton id="btnAbreDialogEfetuarPagamento" value="Efetuar Pagamento">
    <p:ajax process="@form" update=":formConfirmarPagamento, :formAux:growlglobal" disabled="#{!relatorioVendaMB.abreDialogEfetuaPag()}" oncomplete="PF('dialogConfirmarPagamento').show()"/>
</p:commandButton>

<p:commandButton id="btnAbreDialogEfetuarPagamento" value="Efetuar Pagamento" process="@form" update=":formConfirmarPagamento, :formAux:growlglobal, @this" oncomplete="#{not empty relatorioVendaMB.abreDialogEfetuaPag()} ? PF('dialogConfirmarPagamento').show() : return false"/>

...还有许多其他.在某些情况下,将始终显示对话框,而该方法将不会执行,在另一些情况下,将执行该方法,但对话框将永远不会显示...

...And many others. In some, the dialog would always be shown and the method would not be executed, in others the method would be executed but the dialog would never be shown...

有人可以给我一个提示,解决方案,如果可能的话,请解释一下Ajax在这种情况下的工作方式.预先感谢.

Could someone give me a hint, a solution and, if possible, explain how Ajax works in this type of situation. Thanks in advance.

推荐答案

这就是我们要做的...

Here is what we do...

首先,不要为了自己的利益而使用布尔型AJAX和JSF.

First don't use a boolean use AJAX and JSF to your advantage instead.

如果您不想显示对话框,则可以在您的JAVA代码中将"validationFailed"标记为失败".

In your JAVA code you can mark validationFailed if you do not want to show the dialog.

public void abreDialogEfetuaPag () {
    if (vendasAPagar == null || vendasAPagar.isEmpty()){
        MensagensUtil.mensagemAviso("Atenção", "selecione ao menos uma venda");
        FacesContext.getCurrentInstance().validationFailed();
    } 
}

让按钮仅在验证未失败的情况下显示对话框.PrimeFaces会在每个AJAX请求的"args"中移交validationFailed标志的值.

Have your button only show the dialog if validation did not fail. PrimeFaces hands back the value of that validationFailed flag in the "args" of every AJAX request.

<p:commandButton oncomplete="if (!args.validationFailed) PF('dialogConfirmarPagamento').show();" />

这篇关于如何根据条件执行(或不执行)ajax调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆