使用数据页脚中的数字数据自动计算列的总和 [英] Auto calculate total sum of a column with numeric data in datatable footer

查看:413
本文介绍了使用数据页脚中的数字数据自动计算列的总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

数据数据列在< 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆