You may need to reset the page numbering of your report at certain places. For example, if you are generating a weekly or monthly report of all customer invoices, you may want to reset page numbering in your report with the start of every customer. This is sometimes convenient, as it allows you to generate just one report (that is, a report of all invoices issued in a month), which actually consists of many reports (that is, a report for each of your customers).
JasperReports offers an interesting feature known as grouping of records, which you can use to do many tricks in report designing. This recipe demonstrates how to use the grouping feature to reset page numbering in your report.
Refer to installPostgreSQL.txt
file included in the source code download of this chapter to install and run PostgreSQL, which should be up and running before you proceed forward. The source code of this chapter also includes a file named copySampleDataIntoPGS.txt
, which helps you create a database named jasperdb6
and copy sample data for this recipe into the database.
ResetPageCount.jrxml
file from the Task4
folder of the source code of this chapter. The Designer tab of iReport shows the report containing a title, a page header, and empty page footer as shown in the following screenshot:"Page "+$V{PAGE_NUMBER}+" of"
and the second component will have an expression + $V{PAGE_NUMBER}
.Ctrl
key and left-clicking them one by one. Now release the Ctrl
key and align the components horizontally and vertically to the top-right position in the Page Footer section of your report using the arrow keys of your keyboard, as shown in the next screenshot: Order By "CustomerName"
as shown in the next screenshot. Click the OK button. ResetPageCount
in the Report Inspector and click on Add Report Group. Customer
in the Group name field and select CustomerName Field
from the Group by the following report object: option, as shown in the following screenshot: + $V{PAGE_NUMBER}
and go to the Text field properties section of Properties below Palette. Select the Evaluation Time property and change its value to Group
.You have used the grouping feature of JasperReports in this recipe. A group is specified by a<group>
tag with three children named groupExpression, groupHeader
, and groupFooter
. Switch to the XML tab of see what a<group>
tag looks like:
<?xml version="1.0" encoding="UTF-8"?> <jasperReport> <!-- other JasperReports XML tags --> <group name="Customer" isResetPageNumber="true"> <groupExpression> <![CDATA[$F{CustomerName}]]> </groupExpression> <groupHeader> <band height="29"/> </groupHeader> <groupFooter> <band height="37"/> </groupFooter> </group> <pageFooter> <band height="43" splitType="Stretch"> <textField> <reportElement x="426" y="7" width="80" height="20"/> <textElement textAlignment="Right"/> <textFieldExpression class="java.lang.String"> <![CDATA["Page "+$V{PAGE_NUMBER}+" of"]]> </textFieldExpression> </textField> <textField evaluationTime="Group" evaluationGroup="Customer"> <reportElement x="506" y="7" width="40" height="20"/> <textElement/> <textFieldExpression class="java.lang.String"> <![CDATA[" " + $V{PAGE_NUMBER}]]> </textFieldExpression> </textField> </band> </pageFooter> <!-- other JasperReports XML tags --> </jasperReport>
Recall that you chose CustomerName
in the Group by the following report object option in step 10 of the recipe. This is why you can see CustomerName
as part of the content of the<groupExpression>
tag in the code shown above. Grouping is done by whatever field you chose in the Group by the following report object option.
Also notice that the<group>
tag has an attribute named isResetPageNumber
whose value is true
. iReport authored this attribute when you checked the checkbox in step 14 of this recipe. As you can guess, this attribute results in resetting the page numbering.
Notice the first<textField>
tag inside<pageFooter>
tag in the code shown above. This<textField>
tag corresponds to the first component of step 3 (with an expression"Page "+$V{PAGE_NUMBER}+" of")
. This component displays the page numbering and is reset every time a new group starts.
Look at the second<textField>
tag that has an evaluationTime
attribute whose value is Group
. This means that this page numbering field will be evaluated at the end of a group, therefore showing the total number of pages in a group.
18.191.181.252