自动计算数据表页脚中包含数字数据的列的总和 [英] Auto calculate total sum of a column with numeric data in datatable footer
问题描述
当数值数据列在
(或
)中时,有没有办法得到总和表页脚中的摘要行而不在支持 bean 中计算它并将其再次提供为另一个 bean 属性?
When numeric data are listed in a <p:dataTable>
(or <h:dataTable>
), is there any way to get the total sum in the summary row in table's footer without calculating it in the backing bean and providing it as again another bean property?
推荐答案
如果您的环境支持 Java EE 7 的新 EL 3.0(例如 WildFly 8),然后您就可以利用对 Java 8 之类的新支持 EL 中的流和 Lambda 操作(是的,即使在使用 Java 7 时也能工作).
If your environment supports Java EE 7's new EL 3.0 (e.g. WildFly 8), then you can make use of new support for Java 8-like Stream and Lambda operations in EL (yes, this works even when using Java 7).
在您的特定情况下,您可以使用与 Java 8 的 IntStream#sum()
(或 DoubleStream#sum()
,取决于属性类型):
In your particular case, you can use the EL 3.0 equivalent of Java 8's IntStream#sum()
(or DoubleStream#sum()
, depending on property type):
<h:dataTable value="#{bean.items}" var="item">
<h:column>
#{item.number}
</h:column>
<f:facet name="footer">
Total: #{bean.items.stream().map(item->item.number).sum()}
</f:facet>
</h:dataTable>
这篇关于自动计算数据表页脚中包含数字数据的列的总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!