你能从 p:ajax 监听器更新 h:outputLabel 吗? [英] Can you update an h:outputLabel from a p:ajax listener?
问题描述
我正在尝试使用 p:ajax 标记,然后在该侦听器中,我设置了一个名为periodRendered"的值.然后我试图通过来自 p:ajax 标签的更新来更新 h:outputLabel 标签.它没有更新ajaxily,我认为这是因为primefaces ajax 标签无法更新标准jsf outputLabel 标签.
I'm trying to use a p:ajax tag and then in that listener, i'm setting a value called "periodRendered". then i'm trying to update an h:outputLabel tag via an update from the p:ajax tag. It's not updating ajaxily and i'm thinking it's because a primefaces ajax tag can't update a standard jsf outputLabel tag.
我的假设是否正确,是否应该使用更合适的标签代替 h:outputLabel ?
Is my assumption correct and is there a more appropriate tag i should be using instead of h:outputLabel ?
<h:outputLabel for="addProgramTo" value="Add Program To" />
<p:selectOneMenu value="#{ppBacker.grantProgram.grant_project_id}" id="addProgramTo" size="1" styleClass="listBoxMedium">
<p:ajax process=":addProgram:addProgramTo" update=":addProgram:periodGrid, :addProgram:periodLabel" event="change" listener="#{ppBacker.addProgramListener}" />
<f:selectItems value="#{ppBacker.grantProjectDropDownList}" />
</p:selectOneMenu>
<h:outputLabel for="period" value="Period" id="periodLabel" rendered="#{ppBacker.periodRendered}">
推荐答案
您不能更新未呈现的元素,rendered=false
是一种 JSF 方式"以从 DOM 树中删除元素,
You cannot update elements which are not rendered , rendered=false
"is a JSF way to" to remove elements from the DOM Tree ,
它不像 css display:none
或 visibility:hidden
<- 这两个将元素保留在 DOM 树中但隐藏,而 JSF render=false
甚至不会渲染(保留)DOM 树中的元素(您甚至不会在页面的查看源代码"中看到它)
its not like css display:none
or visibility:hidden
<- this two will keep the elements in the DOM tree but hidden , while the JSF rendered=false
wont even render (keep) the element in the DOM tree (you wont even see it in the "view source" of the page)
因此,在您的情况下,您需要使用 `panelGroup' 包装 outputLabel
并更新包装器的 id
So in you case you need to wrap the outputLabel
with `panelGroup' and update the id of the wrapper
<h:panelGroup id="periodLabelWrapper">
<h:outputLabel for="period" value="Period" id="periodLabel" rendered="#{ppBacker.periodRendered}">
</h:panelGroup>
并在 <p:ajax
update
属性中引用包装器(它总是在 DOM 树中)id,如下所示:
and refer to the wrapper (which always be in the DOM tree) id in the <p:ajax
update
attribute, like this:
<p:ajax process=":addProgram:addProgramTo" update=":addProgram:periodGrid, :addProgram:periodLabelWrapper" event="change" listener="#{ppBacker.addProgramListener}" />
另一个解决方案是像这样更新整个表单 <p:ajax update="@form" ...
这样你就不需要包装 outputLabel代码>
Another solution would be the update the entire form like this <p:ajax update="@form" ...
that way you don't need the wrap the outputLabel
关于你的评论问题
@form 如何更新未渲染的元素,但不直接通过 id 定位它们?
how does @form update the un-rendered elements, but targeting them directly through id's does not?
您不能在 update
中定位页面中不存在的元素(rendered=false)它不存在"
You can't target an element in update
which is not present in the page (rendered=false) "its not there"
但是当您使用 update="@form"
或 update="someWrapperID"
时,表单/包装器会重新评估"其所有内部元素,包括带有rendered="#{someBean.someCondition}"
并且条件被重新评估",所以如果结果为 true
元素将被显示...
But when you use update="@form"
or update="someWrapperID"
the form/wrapper "re evaluates" all its inner elements , including the ones with rendered="#{someBean.someCondition}"
and the condition gets "re evaluated" so if it result to true
the elemet will be displayed...
这篇关于你能从 p:ajax 监听器更新 h:outputLabel 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!