PrimeFaces SelectOneMenu Ajax渲染 [英] PrimeFaces SelectOneMenu ajax rendering
问题描述
我的程序出了什么问题?我认为一切都很好.我的changeKeyValue
方法获取选定的值'REFERENCE',并将true
布尔值设置为showReference
.
但是,如果在p:ajax
中使用update="targetPanel"
,则页面无法呈现h:panelGroup
.
当我使用update="entryForm"
时,它可以渲染h:panelGroup
.
What was wrong in my program? I think everything is fine. My changeKeyValue
method get the selected value 'REFERENCE' and set true
boolean value into showReference
.
But, my page cannot render h:panelGroup
if I use update="targetPanel"
in p:ajax
.
It can render h:panelGroup
when I use update="entryForm"
.
我缺少什么?
mypage.xhtml
mypage.xhtml
<h:form id="entryForm">
<p:selectOneMenu value="#{MyBean.keyFactorValue}" required="true" style="width:257px;" id="keyFactorValue">
<f:selectItems value="#{MyBean.selectItemList}" var="type" itemLabel="#{type.label}" itemValue="#{type}"/>
<p:ajax listener="#{MyBean.changeKeyValue}" update="targetPanel" event="change"/>
</p:selectOneMenu>
<h:panelGroup id="targetPanel" rendered="#{MyBean.showReference}">
.......
</h:panelGroup>
</h:form>
KeyFactorValue.java
KeyFactorValue.java
public enum KeyFactorValue {
REFERENCE("Reference"), FORM_TO("From-To"), FIXED("Fixed");
private String label;
private KeyFactorValue(String label) {
this.label = label;
}
public String getLabel() {
return label;
}
}
MyBean.java
MyBean.java
@Scope(ScopeType.CONVERSATION)
@Name("MyBean")
public class MyBean {
private boolean showReference;
public boolean isShowReference() {
return showReference;
}
public KeyFactorValue[] getSelectItemList() {
return KeyFactorValue.values();
}
public void changeKeyValue(AjaxBehaviorEvent e) {
KeyFactorValue type = keyFactor.getKeyFactorValue();
System.out.println("Selected KeyFactorValue : " + type);
switch(type) {
case REFERENCE :{
showReference = true;
break;
}
default :{
showReference = false;
break;
}
}
}
}
推荐答案
您无法Ajax更新HTML中JSF无法呈现的元素. JavaScript(ajax背后的代码)无法通过document.getElementById()
来找到它来替换其内容.
You can't ajax-update an element in HTML which isn't rendered by JSF. JavaScript (the code behind ajax) wouldn't be able to find it by document.getElementById()
in order to replace its content.
您只能ajax更新总是由JSF呈现的HTML中的元素.将有条件渲染的组件包装在始终渲染的另一个组件中,然后对其进行ajax更新.
You can only ajax-update an element in HTML which is always rendered by JSF. Wrap the conditionally rendered component in another component which is always rendered and ajax-update it instead.
<h:panelGroup id="targetPanel">
<h:panelGroup rendered="#{MyBean.showReference}">
...
</h:panelGroup>
</h:panelGroup>