John
John

Reputation: 815

How to create a foor-loop in primefaces/jsf form panel?

I have a primefaces panel, where I want to offer primefaces' p:fileUploader for several elements. During runtime it is not clear, how many items there will be. This is my code:

<h:form>
    <p:panel id="grid" header="">
        <h:panelGrid columns="2" cellpadding="5">

            <c:forEach items="${bean.testCurrent}"
                var="car" varStatus="i">
                <p:outputLabel value="#{car.name}: " for="image${i.index}" />
                <p:column>
                    <p:fileUpload id="image${i.index}"
                        fileUploadListener="#{bean.uploadImage}"
                        mode="advanced" dragDropSupport="true" multiple="false"
                        update="messages" sizeLimit="1000000"
                        allowTypes="/(\.|\/)(gif|jpe?g|png)$/"
                        oncomplete="disableChoosing()"/>
                    <p:growl id="growl" showDetail="true" />
                </p:column>
            </c:forEach>

        </h:panelGrid>
        <p:commandButtonaction="#{bean.save()}" value="Save" />
        <p:commandButton action="#{bean.cancel()}"  value="Cancel" />
    </p:panel>
</h:form>

So my first step was trying it with JSTL's c:forEach. This doesn't work, because JSTL runs during build time of the view and JSF runs during render time of the view component tree.

Is there a possibility to implement a for-loop in the panel or is there any other possibility to display the form elements dynamically?

Upvotes: 1

Views: 7244

Answers (2)

Alan Rusnak
Alan Rusnak

Reputation: 247

You can use <ui:repeat> for that from JSF facelets xmlns:ui="http://java.sun.com/jsf/facelets"

<ui:repeat value="#{bean.testCurrent}" var="car"> ... </ui:repeat>

Examples:

https://www.mkyong.com/jsf2/jsf-2-repeat-tag-example/

How to use <ui:repeat> to iterate over a nested list?

Upvotes: 5

Apostolos
Apostolos

Reputation: 10463

Since you are using primefaces have a look at p:repeat. this is available from version 5.3.5 and above though. Have a look at their example too.

Upvotes: 2

Related Questions