Search code examples
jasper-reportsireport

How to show number of rows on page and the total numbers of rows in report


In JasperReport's report I have been showing data using table

In column footer I want to show total number of records per page

Page 1 Showing 25 records out of 65
Page 2 Showing 50 records out of 65
Page 3 Showing 15 records out of 65

I am using column count to show number of records per page but for total records in report what should I use

Page 1 Showing 25 records out of 25
Page 2 Showing 50 records out of 50
Page 3 Showing 15 records out of 65

Which variable should I use or any condition to be added?


Solution

  • You can easy solve this task with help of built-in variable $V{REPORT_COUNT} and custom variable.

    The working sample

    The jrxml file:

    <?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="no_of_rows_on_page" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="da1c0873-1b22-4592-93a1-c9ffbdcec490">
        <queryString>
            <![CDATA[SELECT name FROM PRODUCT]]>
        </queryString>
        <field name="name" class="java.lang.String"/>
        <variable name="rowsOnPage" class="java.lang.Integer" resetType="Page">
            <variableExpression><![CDATA[$V{rowsOnPage} + 1]]></variableExpression>
            <initialValueExpression><![CDATA[0]]></initialValueExpression>
        </variable>
        <detail>
            <band height="20" splitType="Stretch">
                <textField>
                    <reportElement uuid="dc686db4-cb13-46c2-8420-e9ba198116e3" x="0" y="0" width="185" height="20"/>
                    <textElement/>
                    <textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>
                </textField>
            </band>
        </detail>
        <pageFooter>
            <band height="20">
                <textField>
                    <reportElement uuid="0940bce6-118f-47bc-81f6-ae03ca55ddcb" x="205" y="0" width="112" height="20"/>
                    <textElement/>
                    <textFieldExpression><![CDATA["Showing " + $V{rowsOnPage} + " records out of "]]></textFieldExpression>
                </textField>
                <textField evaluationTime="Report">
                    <reportElement uuid="b5f1da29-7ac8-40a9-889e-e306a0cd26fe" x="317" y="0" width="60" height="20"/>
                    <box leftPadding="2"/>
                    <textElement/>
                    <textFieldExpression><![CDATA[$V{REPORT_COUNT}]]></textFieldExpression>
                </textField>
                <textField>
                    <reportElement uuid="fb9ca24d-844e-4921-8a79-85c2ec2d2888" x="159" y="0" width="46" height="20"/>
                    <textElement/>
                    <textFieldExpression><![CDATA["Page " + $V{PAGE_NUMBER}]]></textFieldExpression>
                </textField>
            </band>
        </pageFooter>
    </jasperReport>
    

    The result

    The output result generated in iReport via preview mode

    Page 2

    Last page

    How it works

    I've add 3 textFields on Page Footer band:

    • 1st one is for showing current page number. The expression is: "Page " + $V{PAGE_NUMBER} and the Evaluation Time is Now

    • 2nd one is for showing count of rows on current page. The expression is: `"Showing " + $V{rowsOnPage} + " records out of "' and the Evaluation Time is Now

    • 3rd one is for showing total rows in whole report. The expression is: $V{REPORT_COUNT} and the Evaluation Time is Report.

    As you can see I've add custom variable rowsOnPage for counting number of rows on page.

    The variable definition:

    • Expression: $V{rowsOnPage} + 1
    • Type: Integer
    • Initial value: 0
    • Reset type: Page

    See details about:
    Variables
    Built-in variables in JasperReports Ultimate Guide