p:commandButton onclick中的EL表达式不会在ajax请求上更新/重新呈现? [英] EL expression inside p:commandButton onclick does not update/re-render on ajax request?

查看:172
本文介绍了p:commandButton onclick中的EL表达式不会在ajax请求上更新/重新呈现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 commandButton onclick 属性里面有一些EL依赖的Javascript。更具体地说,这是一段代码:

The onclick attribute of my commandButton has some EL dependent Javascript inside. To be more specific, here is that piece of code:

<p:commandButton
    onclick="javascript: if('#{userBean.user.friendList.size() gt 0}' == 'true') deleteFriendsConfirmDialog.show(); else friendsAlreadyDeletedErrorDialog.show();"
    value="Delete all friends?" />

deleteFriendsConfirmDialog 清除朋友和更新列表 @form 。朋友列表,commandButton和对话都是这种形式。

deleteFriendsConfirmDialog clears the list of friends and updates the @form. The list of friends, commandButton and the dialogs are all in that form.

所以我点击按钮,出现确认对话框(因为朋友列表的长度是gt 0),我确认清单清空,视图更新。但是,当我再次单击删除所有朋友?按钮时,将再次出现确认对话框。由于列表的长度现在为0,我反而希望显示错误对话框。

So I click the button, confirmation dialog comes up (because the length of friends' list is gt 0), I confirm and the list is emptied, the view is updated. However, when I click the Delete all friends? button once more, the confirmation dialog comes up again. Since the length of the list is now 0, I instead expect the error dialog to show.

我想,这是因为写在 onclick 未更新(虽然按钮位于表单中)。

That, I guess, is because the Javascript written inside onclick is not updated (although the button is in the form).

修改:更改#{userBean.user.friendList.size()gt 0} to #{not empty userBean.user.friendList} doesn'不工作。

Changing #{userBean.user.friendList.size() gt 0} to #{not empty userBean.user.friendList} doesn't work neither.

为什么?这里有什么问题?

How come? What's wrong here?

任何帮助表示赞赏。 :)

Any help appreciated. :)

推荐答案

确实如此。 PrimeFaces(和标准JSF)不会在每个请求的基础上重新评估* 属性上的中的EL。它只发生在每个视图的基础上。然而,RichFaces在< a4j:xxx> 组件中执行此操作。

Indeed. PrimeFaces (and standard JSF) does not re-evaluate the EL in on* attributes on a per-request basis. It only happens on a per-view basis. RichFaces however does that in <a4j:xxx> components.

您需要以不同方式解决问题。我建议使用< p:dialog> visible 属性。

You need to solve the problem differently. I suggest to use the visible attribute of the <p:dialog> instead.

<h:form>
    ...
    <p:commandButton value="Delete all friends?" update=":deleteDialogs" />
</h:form>
...
<h:panelGroup id="deleteDialogs">
    <p:dialog id="deleteFriendsConfirmDialog" visible="#{facesContext.postback and not empty userBean.user.friendList}">
        ...
    </p:dialog>
    <p:dialog id="friendsAlreadyDeletedErrorDialog" visible="#{facesContext.postback and empty userBean.user.friendList}">
        ...
    </p:dialog>
</h:panelGroup>

另一种方法是使用PrimeFaces' RequestContext 在bean的action方法中,它允许你以编程方式在bean的action方法中执行JavaScript代码(虽然这与控制器有点过多地与视图,IMO结合)。

An alternative is to use PrimeFaces' RequestContext in the bean's action method which allows you to execute JavaScript code programmatically in bean's action method (although this tight-couples the controller a bit too much with the view, IMO).

<p:commandButton value="Delete all friends?" action="#{userBean.deleteAllFriends}" />

with

RequestContext context = RequestContext.getCurrentInstance();

if (!user.getFriendList().isEmpty()) {
    context.execute("deleteFriendsConfirmDialog.show()");
} else {
    context.execute("friendsAlreadyDeletedErrorDialog.show()");
}






不相关对于具体问题,您的原始 onclick 虽然在您的特定情况下不起作用,但显示了一些不良做法。 javascript:伪协议是多余的。这已经是默认值了。去掉它。对 =='true'的测试也是多余的。去掉它。只需让EL直接打印 true false 。以下是正确的语法(再次,这不能解决您的问题,只是为了您的信息)


Unrelated to the concrete problem, your original onclick, while it does not work out in your particular case, shows some poor practices. The javascript: pseudoprotocol is superfluous. It's the default already. Remove it. Also the test against == 'true' is superfluous. Remove it. Just let the EL print true or false directly. The following is the proper syntax (again, this does not solve your problem, just for your information)

<p:commandButton
    onclick="if (#{not empty userBean.user.friendList}) deleteFriendsConfirmDialog.show(); else friendsAlreadyDeletedErrorDialog.show();"
    value="Delete all friends?" />

如果您使用的是RichFaces的< a4j:commandButton> ;

It would have worked if you were using RichFaces' <a4j:commandButton>.

<a4j:commandButton
    oncomplete="if (#{not empty userBean.user.friendList}) deleteFriendsConfirmDialog.show(); else friendsAlreadyDeletedErrorDialog.show();"
    value="Delete all friends?" />

这篇关于p:commandButton onclick中的EL表达式不会在ajax请求上更新/重新呈现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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