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
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>
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With