Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

For-each loop in rich:subTable

Is it possible to use a forEach loop in the subTable component or columns, in Richfaces 3.3? I need to render dynamic number of columns, but without success.

Example:

<rich:dataTable 
       cellpadding="0" cellspacing="0" 
       width="700" border="1" var="serviceCharge"
       value="#{group.serviceCharges}">
    <rich:column colspan="3">
        <h:outputText value="#{group.name},#{serviceCharge.code}" />
    </rich:column>
    <rich:subTable var="priceType" value="#{serviceCharge.priceTypes}">
        <rich:column colspan="#{group.priceLevels.size}">
            <b><h:outputText value="#{priceType.name}" /></b>
        </rich:column>
        <rich:subTable var="priceLevelItem" value="#{priceType.priceLevels}">
            <rich:column colspan="3">
                <h:outputText value="Qty" />
            </rich:column>
            <c:forEach items="#{priceType.priceLevels}" var="priceLevelItem">
                <rich:column colspan="3">
                    <h:outputText value="#{priceLevelItem.id},#{priceLevelItem.qty}" />
                </rich:column>                              
            </c:forEach>
         </rich:subTable>                                                   
        <rich:subTable var="priceLevelItem" value="#{priceType.priceLevels}">
            <rich:column colspan="3">
                <h:outputText value="Amount" />
            </rich:column>
            <rich:column colspan="3">
                <h:outputText value="#{priceLevelItem.id},#{priceLevelItem.amount}" />
            </rich:column>
        </rich:subTable>                                                    
    </rich:subTable>
</rich:dataTable>

Thanks

like image 395
Nikola Avatar asked Jan 19 '26 20:01

Nikola


2 Answers

Yes, you can.

You could use the following code to define the column list:

<ui:param name="fields" value="colname1, colname2, colname3"/>

And iterate over it in the column section of the dataTable:

<rich:dataTable binding="#{backingBean.table}"
        value="#{backingBean.list}" var="row">
    <ui:insert name="extraColumnsFirst"></ui:insert>
    <f:facet name="header">
        <rich:columnGroup>
            <ui:insert name="extraColumnsHeaderFirst"/>
            <c:forEach items="${fn:split(fields, ',')}"
                    var="fieldName" varStatus="status">
                <rich:column>
                    <h:outputText  value="${fieldName}" />
                </rich:column>
            </c:forEach>
        </rich:columnGroup>
    </f:facet>
    <c:forEach items="${fn:split(fields, ',')}"
            var="fieldName" varStatus="status">
        <rich:columnid="column_${fieldName}_${status.index}">
            <f:facet name="header"></f:facet>
            <h:outputText id="${fieldName}_${status.index}"
                    value="${row[fieldName]}">
            </h:outputText>
        </rich:column>
    </c:forEach>
    <ui:insert name="extraColumnsLast">
    </ui:insert>
    <f:facet name="footer">
        <rich:datascroller id="ds" renderIfSinglePage="false">
        </rich:datascroller>
    </f:facet>
</rich:dataTable>
like image 97
dov.amir Avatar answered Jan 21 '26 09:01

dov.amir


Yes, c:forEach can be used to generate dynamic number of columns (for both rich:dataTable and rich:subTable).

In your example it does not work because you are trying to refer var priceType which is not defined (c:forEach is a TagHandler so it is trying to evaluate priveType when the tree is being built; rich:dataTable is a Component and it defines the var priveType only on render response).

For more information on the matter you could read the following article: TagHandler vs Component.

like image 32
Andrey Avatar answered Jan 21 '26 10:01

Andrey