Auto calculate total sum of a column with numeric data in datatable footer
Asked Answered
C

1

9

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?

Cellobiose answered 8/12, 2013 at 17:0 Comment(2)
did you try javascript?Hardness
How can I use Javascript?Cellobiose
N
18

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).

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>
Nutshell answered 26/8, 2014 at 18:56 Comment(1)
Works well, but not when you filter the datatable. How can you manage it ?Callaghan

© 2022 - 2024 — McMap. All rights reserved.