基于ID的几个元素的ajax更新。

时间:2023-02-04 20:08:58

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”的元素就排在第二位。如果页面上的元素不是严格的,那么第二个带有“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?

是正常的行为还是我错过了一些参数?

1 个解决方案

#1


65  

If the to-be-updated component is not inside the same NamingContainer component (ui:repeat, h:form, h:dataTable, etc), then you need to specify the "absolute" client ID. Prefix with : (the default NamingContainer separator character) to start from root.

如果要更新的组件不在同一个NamingContainer组件中(ui:repeat, h:form, h:dataTable,等等),那么您需要指定“绝对”客户端ID。

<p:ajax process="@this" update="count :subTotal"/>

To be sure, check the client ID of the subTotal component in the generated HTML for the actual value. If it's inside for example a h:form as well, then it's prefixed with its client ID as well and you would need to fix it accordingly.

可以确定的是,检查生成的HTML中的subTotal组件的客户ID是否为实际值。如果它在内部,例如h:表单,那么它也会以它的客户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.

IDs的空间分离更被推荐为 不支持逗号分隔,而启动器则会被混淆。

#1


65  

If the to-be-updated component is not inside the same NamingContainer component (ui:repeat, h:form, h:dataTable, etc), then you need to specify the "absolute" client ID. Prefix with : (the default NamingContainer separator character) to start from root.

如果要更新的组件不在同一个NamingContainer组件中(ui:repeat, h:form, h:dataTable,等等),那么您需要指定“绝对”客户端ID。

<p:ajax process="@this" update="count :subTotal"/>

To be sure, check the client ID of the subTotal component in the generated HTML for the actual value. If it's inside for example a h:form as well, then it's prefixed with its client ID as well and you would need to fix it accordingly.

可以确定的是,检查生成的HTML中的subTotal组件的客户ID是否为实际值。如果它在内部,例如h:表单,那么它也会以它的客户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.

IDs的空间分离更被推荐为 不支持逗号分隔,而启动器则会被混淆。