[英]JSF 'total' variable for datatable
I have read the previous post: JSF 'total' variable something like c:set in JSTL .我已阅读上一篇文章: JSF 'total' 变量类似于 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.
尽管答案表明总数应来自支持 bean,但我确实需要在 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>.
数据来自 List<Entity> 类型的 JPA get。 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.
如果我在后备 bean 中进行了总计,我需要迭代列表,创建一个数据 model 仅用于运行总计属性。 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.但它不起作用,每行 ${sum} 重置为零。 Is there another way, except making a custom component?
除了制作自定义组件之外,还有其他方法吗?
This still can be solved using a method in the backing bean:这仍然可以使用支持 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.不优雅,但它有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.