自动计算数据表页脚中具有数字数据的列的总和 [英] Auto calculate total sum of a column with numeric data in datatable footer
问题描述
当数值数据列在 <p:dataTable>
(或 <h:dataTable>
)中时,有没有办法在表格页脚中的摘要行,而不在支持 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 的 EL 3.0 等效版本 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屋!