开发者

JSF 'total' variable for datatable

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>
 开发者_C百科   </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?


This still can be solved using a method in the backing bean:

public class MyBackingBean {

 private Double runningTotal = 0.0;

 public Double getRunningTotal(Item item) {
   Double result = runningTotal;
   runningTotal += item.getAmount();
   return result;
 }

}

Then in your view, use this to display the running total:

<td><h:outputText value="#{xxxBean.getRunningTotal(item)}" /></td>

Not elegant, but it works.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜