Jump to content
We've recently updated our Privacy Statement, available here ×
  • How to keep the last report record and report summary information together even when pagination occurs


    Tom C
    • Version: v5.6.0 Product: iReport Designer

    Problem

    When generating a report, the JasperReports engine checks the available spaces left in the current report page to determine whether a new page is needed in order to fit the detail band height where the new row is fetched and placed in the report.

    Since the user usually places report tally information in the summary band, and the available space on the current page calculation does not take this band into consideration, it is possible for the last report record to be  at the bottom of a page, and the report tally information placed on a new page.

    While normally this will not present a problem, it sometimes looks odd if the summary information is merely a one line report record count in the new page. In such case, the user can use the following approach to make the last report record and report summary information stick together even when a page break takes place in the report layout:

    Solution

    1. Get the total row count from the report query. We will need this information to steer JR into using different detail bands to render the report prior to the first detail row is processed.

    2. Add the Print When condition to your existing report detail section

      $V{REPORT_COUNT} < $F{total_row}
      
    3. Add a second detail section with a larger band height to contain the same contents as the original report detail section plus the report total placed below the original detail content. The print when expression for the new detail section is

      $V{REPORT_COUNT} ==$F{total_row}
      

    Original report design

    1-original-design(1).png.e5337e0a5a4544dacb7b23d75d545429.png

    New report design

    2-new-design.png.1aee5d3ae7ea0ab08dd13f513d630a71.png

    This will allow JasperReports to use the second detail band to calculate the page break and place all contents in this band together when placing it into the report layout.

    The following is a sample report to demonstrate this approach. You can test run it with any Postgres data source and use parameter1 to experiment with different number of rows generated by the SQL query to see how it works under various boundary conditions.

    <?xml version="1.0" encoding="UTF-8"?>
    <jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="report-sticky-tally-TTC" language="groovy" pageWidth="612" pageHeight="792" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="9cee41ce-4f4c-4ae1-835b-6f010cef6fd6">
        <property name="ireport.zoom" value="1.0"/>
        <property name="ireport.x" value="0"/>
        <property name="ireport.y" value="0"/>
        <parameter name="parameter1" class="java.lang.Integer">
            <defaultValueExpression><![CDATA[21]]></defaultValueExpression>
        </parameter>
        <queryString>
            <![CDATA[WITH RECURSIVE t(n) AS (
        VALUES (1)
      UNION ALL
        SELECT n+1 FROM t WHERE n < $P{parameter1}
    )
    
    select n, (select count(*) from t) as total_row from t;]]>
        </queryString>
        <field name="n" class="java.lang.Integer"/>
        <field name="total_row" class="java.lang.Long"/>
        <background>
            <band splitType="Stretch"/>
        </background>
        <title>
            <band height="30" splitType="Stretch">
                <staticText>
                    <reportElement x="0" y="0" width="572" height="25" uuid="9bc5d893-07f3-4fe8-8e30-53d38bb95f51"/>
                    <textElement textAlignment="Center">
                        <font size="12" isBold="true"/>
                    </textElement>
                    <text><![CDATA[Report for Sticky Tally]]></text>
                </staticText>
            </band>
        </title>
        <pageHeader>
            <band height="30" splitType="Stretch">
                <staticText>
                    <reportElement x="0" y="0" width="572" height="25" uuid="922ed4f3-70af-4cd8-b569-d234045b3d10"/>
                    <textElement textAlignment="Center">
                        <font size="12" isBold="true"/>
                    </textElement>
                    <text><![CDATA[Report Page]]></text>
                </staticText>
            </band>
        </pageHeader>
        <columnHeader>
            <band height="30" splitType="Stretch">
                <staticText>
                    <reportElement x="239" y="5" width="100" height="20" uuid="20c722a5-0c3c-4373-ab19-2c644fcae5b3"/>
                    <textElement textAlignment="Center"/>
                    <text><![CDATA[Column count]]></text>
                </staticText>
            </band>
        </columnHeader>
        <detail>
            <band height="30" splitType="Stretch">
                <printWhenExpression><![CDATA[$V{REPORT_COUNT}.intValue()<$F{total_row}.intValue()]]></printWhenExpression>
                <textField pattern="###0">
                    <reportElement x="239" y="5" width="100" height="20" uuid="b7d46f90-1f98-4cb2-aab3-a8bc8db3e3cf"/>
                    <textElement textAlignment="Center"/>
                    <textFieldExpression><![CDATA[$F{n}]]></textFieldExpression>
                </textField>
            </band>
            <band height="50">
                <printWhenExpression><![CDATA[$V{REPORT_COUNT}.intValue()==$F{total_row}.intValue()]]></printWhenExpression>
                <textField pattern="###0">
                    <reportElement x="239" y="5" width="100" height="20" uuid="7a44c310-8b4e-45e0-84d7-02a017f8588f"/>
                    <textElement textAlignment="Center"/>
                    <textFieldExpression><![CDATA[$F{n}]]></textFieldExpression>
                </textField>
                <textField pattern="###0">
                    <reportElement x="239" y="30" width="100" height="20" uuid="90595f8e-b80a-44ae-ab73-638dc38601f5"/>
                    <textElement textAlignment="Center">
                        <font size="12" isBold="true"/>
                    </textElement>
                    <textFieldExpression><![CDATA[$V{REPORT_COUNT}]]></textFieldExpression>
                </textField>
            </band>
        </detail>
        <columnFooter>
            <band splitType="Stretch"/>
        </columnFooter>
        <pageFooter>
            <band height="30" splitType="Stretch">
                <staticText>
                    <reportElement x="274" y="0" width="21" height="20" uuid="ace1321a-bb57-41d9-8bd1-260cdd0bf951"/>
                    <text><![CDATA[ of ]]></text>
                </staticText>
                <textField pattern="###0">
                    <reportElement x="174" y="0" width="100" height="20" uuid="bf077956-bff9-46f7-a41c-f302ffe60c8d"/>
                    <textElement textAlignment="Right"/>
                    <textFieldExpression><![CDATA[$V{PAGE_NUMBER}]]></textFieldExpression>
                </textField>
                <textField evaluationTime="Report" pattern="###0">
                    <reportElement x="295" y="0" width="100" height="20" uuid="8fa00844-c0f3-4ed6-bdeb-b65c4d9bc1da"/>
                    <textElement textAlignment="Left"/>
                    <textFieldExpression><![CDATA[$V{PAGE_NUMBER}]]></textFieldExpression>
                </textField>
            </band>
        </pageFooter>
    </jasperReport>
    

     


    TTC-20140830

     

    1-original-design(1).png.53b5b05806bded757e8dc339e74af03a.png

    2-new-design.png.e333e82db6a015f9caccc610767b4672.png


    User Feedback

    Recommended Comments



    Guest
    This is now closed for further comments

×
×
  • Create New...