Search code examples
jasper-reports

jasper report table show previous page total


I am using Jaspersoft® Studio 6.8.0.

I want to have a table which can show previous page total, take following table for example, assume this table has 3 pages:

                     grade
                     10
                     20
        sub total    30
                             page 1
                     grade
previous page total  30
                     40
                     50
          sub total  90
                            page 2
                     grade
previous page total  90
                     60
                     70
         sub total   130
                            page 3

that is show previous page total from 2nd page to last page.

How to achieve that? thanks!


Solution

  • One way to do it is to create a variable that holds a mutable value set as a side effect by an expression of the text that displays the total on the previous page.

    E.g. something like this:

        <variable name="TotalVariable" class="java.lang.Integer" calculation="Sum" resetType="Page">
            <variableExpression>$F{Column}</variableExpression>
        </variable>
        <variable name="PreviousTotalHolder" class="java.util.concurrent.atomic.AtomicInteger" calculation="System">
            <initialValueExpression>new java.util.concurrent.atomic.AtomicInteger()</initialValueExpression>
        </variable>
        ...display the previous total in the new page
                            <textField>
                                <reportElement .../>
                                <textFieldExpression>"Previous total " + $V{PreviousTotalHolder}.get()</textFieldExpression>
                            </textField>
        ...use a dummy property in the page footer to set the total into the holder object
                            <textField>
                                <reportElement x="0" y="0" width="90" height="15" uuid="a1ab288e-ae4b-4f12-83a5-e30486bb30d5">
                                    <propertyExpression name="foo">Integer.toString($V{PreviousTotalHolder}.getAndSet($V{TotalVariable}))</propertyExpression>
                                </reportElement>
                                <textFieldExpression>"Total " + $V{TotalVariable}</textFieldExpression>
                            </textField>
    

    There might be other ways to do it depending on the exact design of you report (for instance depending on whether you have a table component or a tabular band report).