I have read the previous post: JSF 'total' variable something like c:set in JSTL. Although the answer suggests that the total should come from the backing bean, I have genuine need to do it in the facelet. For my case, I want to display a bank book type datatable, with each row consisting of a date, a description, an amount and a running total. The data come from a JPA get of type List<Entity>. If I did the total in the backing bean I need to iterate the List, create a data model solely for the purpose of a running-total property. This is inefficient indeed.
I tried:
<c:set var="sum" value="0.0" scope="view" />
<table>
<ui:repeat value="#{xxxBean.items}" var="item">
<tr>
<td><h:outputText value="#{item.date1}" /></td>
<td><h:outputText value="#{item.desc}" /></td>
<td><h:outputText value="#{item.amount}" /></td>
<c:set var="sum" value="${sum+item.amount}"/>
<td><h:outputText value="${sum}" /></td>
</tr>
</ui:repeat>
</table>
but it does not work, ${sum} resets to zero for each row. Is there another way, except making a custom component?