JSF(Primefaces) 通过 ID 更新多个元素的 ajax
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4474789/
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
JSF(Primefaces) ajax update of several elements by ID's
提问by sergionni
One more question concerning JSF.Particularly, Primefaces.
Have following problem with ajax update of elements by id's at same time.
If elements on page goes one by one ,that ajax update performs ok:
还有一个关于 JSF 的问题。特别是 Primefaces。
同时通过 id 更新元素的 ajax 存在以下问题。如果页面上的元素一个接一个,则 ajax 更新执行正常:
<ui:repeat value="#{showProducts.inCart}" var="cart">
<td><p:spinner min="0" value="#{cart.count}" immediate="true">
<p:ajax process="@this" update="count,subTotal"/></p:spinner></td>
<td><h:outputText value="#{cart.totalPrice}" id="count"/></td>
<h:outputText value="#{showProducts.subTotal}" id="subTotal"/>
</ui:repeat>
Here element with id "count" goes first,then element with id "subtotal" goes second. In case,elements on page are not strictly one by one,that second element with "subtotal" id is not updated:
这里 id 为“count”的元素排在第一位,然后 id 为“subtotal”的元素排在第二位。如果页面上的元素不是严格一个一个,则不会更新具有“小计”id 的第二个元素:
<ui:repeat value="#{showProducts.inCart}" var="cart">
<td><p:spinner min="0" value="#{cart.count}" immediate="true">
<p:ajax process="@this" update="count,subTotal"/></p:spinner></td>
<td><h:outputText value="#{cart.totalPrice}" id="count"/></td>
<td><h:outputText value="#{cart.place}" /></td>
</ui:repeat>
<h:outputText value="#{showProducts.subTotal}" id="subTotal"/>
Is it normal behaviour or I miss some parameters?
这是正常行为还是我错过了一些参数?
回答by BalusC
If the to-be-updated component is not inside the same NamingContainercomponent (ui:repeat, h:form, h:dataTable, etc), then you need to specify the "absolute" client ID. Prefix with :(the default NamingContainerseparator character) to start from root.
如果待更新的组件是不一样的内部NamingContainer组件(ui:repeat,h:form,h:dataTable等),那么你需要指定“绝对”客户端ID。以:(默认NamingContainer分隔符)为前缀从根开始。
<p:ajax process="@this" update="count :subTotal"/>
To be sure, check the client ID of the subTotalcomponent in the generated HTML for the actual value. If it's inside for example a h:formas well, then it's prefixed with its client ID as well and you would need to fix it accordingly.
可以肯定的是,检查subTotal生成的 HTML 中组件的客户端 ID以获得实际值。如果它h:form也在内部,例如 a ,那么它的前缀也是它的客户端 ID,您需要相应地修复它。
<p:ajax process="@this" update="count :formId:subTotal"/>
Space separation of IDs is more recommended as <f:ajax>doesn't support comma separation and starters would otherwise get confused.
更推荐使用空格分隔 ID,因为<f:ajax>它不支持逗号分隔,否则初学者会感到困惑。

