Wizard Sultan
Wizard Sultan

Reputation: 928

Assign unique ids to <div> inside <c:forEach>

I want to assign a unique id for the <div> enclosed in the <c:forEach>. Whenever the page is rendered all the <div>s generated by the <c:forEach> have the same id. Is there any way to assign unique id for all the divs generated by the <c:forEach>? I have tried using <ui:repeat> but I was having issues with it so I decided to stick with <c:forEach>.

Facelet:

<c:forEach var="p" items="#{statusBean.statusList}">
    <h:form>
        <div class="status">
            // Content
        </div>
    </h:form>
</c:forEach>

Upvotes: 3

Views: 3742

Answers (2)

Elias Dorneles
Elias Dorneles

Reputation: 23876

Use the c:forEach attribute varStatus to define a variable that will contain the status of the loop. Then you can use it in your template like this:

<c:forEach var="p" items="#{statusBean.statusList}" varStatus="loop">
        <h:form>
            <div class="status_#{loop.count}">
            // Content
            </div>
        </h:form>
</c:forEach>

You can also use #{loop.index} if you want it initiated with 0.

Upvotes: 7

jahroy
jahroy

Reputation: 22692

Try using a <h:panelGroup> or another JSF component to render your divs.

This will cause unique IDs to get generated for you.

Upvotes: 0

Related Questions