user3663882
user3663882

Reputation: 7357

Using ui:repeat to iterate over columns

I'm in Mojarra 2.1.29.

I tried to iterate over the collection to produce amount of rows equals to the size of the collection. Here what I've tried:

<rich:dataTable id="table"
                       var="partner"
                       rows="10"
                       value="#{testBean.k}">

    <ui:repeat var="name" value="#{testBean.cols}"> 
           <rich:column>
               <f:facet name="header">
                   <h:outputText value="#{name}"/>
               </f:facet>
               <h:outputText value="#{m.get(partner)}"/>
               <f:facet name="footer">
                   <h:outputText value="#{name}}"/>
               </f:facet>
           </rich:column>
    </ui:repeat>
</rich:dataTable>

testBean.cols was initalized as follows:

public class TestBean{
    private List<String> cols = new ArrayList<>();

    public TestBean() {
        cols.add("Col 1");
        cols.add("Col 2");
    }
}

It didn't work. As the result a got a table with 0 columns. What was wrong and is there a way to fix that?

I've made sure that the expression #{partnerListController.cols} resolved to a non-empty list by putting it in the markup and getting [Col 1, Col 2].

Why can't I iterate over such lists?

Upvotes: 0

Views: 1046

Answers (1)

Zim
Zim

Reputation: 1483

Using c:foreach instead of ui:repeat as suggested by @Kukeltje will probably work, but the proper way to create dynamic columns by iterating over a Collection would rather be using the <rich:columns> component.

From https://docs.jboss.org/richfaces/latest_3_3_X/en/devguide/html/rich_columns.html:

The <rich:columns> component gets a list from data model and outputs a corresponding set of columns inside <rich:dataTable> on a page. It is possible to use "header" and "footer" facets with <rich:columns> component.

The "value" and "var" attributes are used to access the values of collection.

<rich:dataTable value="#{testBean.k}" var="partner" id="table" rows="10">
   <rich:columns value="#{testBean.cols}" var="name">
           <f:facet name="header">
               <h:outputText value="#{name}"/>
           </f:facet>
           <h:outputText value="#{m.get(partner)}"/>
           <f:facet name="footer">
               <h:outputText value="#{name}"/>
           </f:facet>>
   </rich:columns>
</rich:dataTable>

Upvotes: 2

Related Questions