Search code examples
jsf-2primefacesmyfaces

Does not work properly id generation on ui:repeat My faces 2.2.9 + prime faces 5.3


Actually I'm working with Myfaces version 2.2.9 and I've the following structure to generate any panel according with a specific number selected by the user.

...
<ui:repeat value="#{garajes}" var="garaje" varStatus="loop">
<p:panelGrid >
    <h:outputLabel value="Numero de garaje #{loop.index+1}: " />
    <h:outputLabel value="Matricula  #{loop.index+1}: " />

    <p:inputText   value="#{garaje.numeroGaraje}"   maxlength="5" >
    </p:inputText>

    <p:inputText id="matriculaInmobiliariaGaraje-#{loop.index+1}"   value="#{garaje.matriculaInmobiliaria}" 
        maxlength="20">
    </p:inputText>

    ...

</p:panelGrid>
</ui:repeat>
....

So, when is rendered the above code the identifiers are weird, has another things like the following image:

enter image description here

So I don't know how to remove this weird things inside of id

Note: I need a specific id to update another component inside of the loop.

What can I do to get a right identifiers inside of ui:repeat?


Solution

  • As to the concrete problem, just give all NamingContainer components a fixed ID. This includes the <ui:repeat> itself.

    <ui:repeat id="garajes" ...>
    

    As to the concrete requirement, you're overcomplicating things. A NamingContainer will all by itself worry about uniqueness of IDs of children. Your attempt in id="matriculaInmobiliariaGaraje-#{loop.index+1}" won't work at all as #{loop} variable isn't available during view build time, when the component is being instantiated with id. Get rid of it. You can inside a NamingContainer just use a relative ID to reference another component in the same NamingContainer.

    <ui:repeat ...>
        <p:inputText id="foo" />
        <p:inputText ...><p:ajax ... update="foo" /></p:inputText>
    </ui:repeat>
    

    This will work just fine.

    See also: