如何动态引用元素ID

如何动态引用元素ID

问题描述:

我想使用tomahawk selectOneRadio传播单选按钮。我的ID看起来像这样如何动态引用元素ID

<rich:dataTable id="carTable" value="#{cars}" var="car"> 
    <rich:column> 
     <s:decorate id="types" template="/layout/display-text.xhtml"> 
      <t:selectOneRadio id="models" value="#{car.model}" layout="spread"> 
       <f:selectItems value="#{models}" /> 
      </t:selectOneRadio> 
      <h:panelGrid columns="2"> 
       <a:repeat value="#{models}" rowKeyVar="index"> 
        <t:radio for="car:carTable:0:types:models" index="#{index}"></t:radio> 
       </a:repeat> 
      </h:panelGrid> --> 
     </s:decorate> 
    </rich:column> 
</rich:dataTable> 

我在检查元素后引用了id。但是这不起作用。因为每次迭代单选按钮ID都会变化。如何推荐在T一个id:无线电

+0

我不做RichFaces/Seam,但不只是`for =“models”`工作?它会在没有它们的情况下工作。 – BalusC 2011-01-22 04:15:43

documentation for t:radio说:

引用扩展selectOneRadio组件的ID。使用标准UIComponent.findComponent()搜索算法将此值解析为特定组件。

我猜a:repeatNamingContainer,所以使用"models"将无法​​正常工作。您可以使用t:selectOneRadio组件的client identifier

像这样:

<t:selectOneRadio id="models" value="#{car.model}" layout="spread" 
    binding="#{requestScope.modelComponent}"> 
    <f:selectItems value="#{models}" /> 
</t:selectOneRadio> 
<h:panelGrid columns="2"> 
    <a:repeat value="#{models}" rowKeyVar="index"> 
    <t:radio for="#{requestScope.modelComponent.clientId}" index="#{index}" /> 
    </a:repeat> 
</h:panelGrid> 

注意事项:

  1. 此使用密钥 “modelComponent” 结合所述组分与请求范围图;你需要确保这种情况不会有别的东西碰撞
  2. 解决modelComponent.clientId需要JSF 2.0(JEE6)以上

here更多细节;使用旧版本;等等。