p:ajax 更新不适用于 p:selectBooleanCheckbox

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24296402/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 10:25:37  来源:igfitidea点击:

p:ajax update not working with p:selectBooleanCheckbox

ajaxjsfprimefaces

提问by Cenobyte321

I'm using JSF 2 and Primefaces 4 and have the following issue:

我正在使用 JSF 2 和 Primefaces 4 并且有以下问题:

I've got the following code in my XHTML:

我的 XHTML 中有以下代码:

<h:form id="form">
<table>  
    <tr id="formats">
        <td>Formats</td>
        <td>
            <p:selectBooleanCheckbox value="#{bean.entity.formatted}">
                <p:ajax event="change" update="formatsSelect" /> 
            </p:selectBooleanCheckbox>
            <p:selectOneMenu id="formatsSelect" rendered="#{bean.entity.formatted}">
                <f:selectItems value="#{bean.formats}" />
            </p:selectOneMenu> 
        </td>
    </tr>
</table>
</h:form>

It outputs a checkbox and a select menu and what I expect is that when I check the checkbox the select menu should appear and should disappear when I uncheck it.... but nothing happens, I check and uncheck it and the select menu is unaffected.

它输出一个复选框和一个选择菜单,我期望的是,当我选中复选框时,选择菜单应该出现,当我取消选中它时应该消失......但没有任何反应,我选中并取消选中它并且选择菜单不受影响.

In theory this should work since the selectBooleanCheckbox value is bound to the entity.formatted boolean value and the rendered value in the selectOneMenu is bound to the entity.formatted value and the p:ajax points to the correct id in the update attribute and the event is correct. This last bit I know since I created a listener in my bean that printed the value of formatted:

理论上这应该有效,因为 selectBooleanCheckbox 值绑定到 entity.formatted 布尔值,并且 selectOneMenu 中呈现的值绑定到 entity.formatted 值,并且 p:ajax 指向更新属性和事件中的正确 id是正确的。这是我知道的最后一点,因为我在我的 bean 中创建了一个监听器来打印 formatted 的值:

public void changeListener(){
    System.out.println(this.entity.isFormatted());
}

And changed the p:ajax to:

并将 p:ajax 更改为:

<p:ajax event="change" update="formatsSelect" listener="#{bean.changeListener}" /> 

And it printed the value of formatted in the console. What am I doing wrong?

它在控制台中打印了 formatted 的值。我究竟做错了什么?

回答by Kishor Prakash

Since you used renderedon the component (p:selectOneMenu id="formatsSelect") and updating the same component, it wont work.

由于您rendered在组件 ( p:selectOneMenu id="formatsSelect") 上使用并更新了相同的组件,因此它不起作用。

Because that component might not have been added to/present in component tree by the time you are updating.

因为在您更新时,该组件可能尚未添加到/出现在组件树中。

So use a h:panelGrouparound it and update it and use renderedon p:selectOneMenu.

所以h:panelGroup在它周围使用 a并更新它并使用renderedon p:selectOneMenu

<p:selectBooleanCheckbox value="#{bean.entity.formatted}">
      <p:ajax event="change" update="formatsSelectPG" /> 
</p:selectBooleanCheckbox>

<h:panelGroup id="formatsSelectPG">
      <p:selectOneMenu id="formatsSelect" rendered="#{bean.entity.formatted}">
            <f:selectItems value="#{bean.formats}" />
       </p:selectOneMenu> 
</h:panelGroup>