This article by Bilal Siddiqui, author of JasperReports 3.6 Development Cookbook, teaches you to build a simple one-page table of contents(TOC) for your multi-page reports.
(For more resources on JasperReports, see here.)
Getting ready
Refer to the installPostgreSQL.txt file included in the source code download (chap5) to install and run PostgreSQL, which should be up and running before you proceed. The source code 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.
How to do it...
- Open the SimpleTOCReport.jrxml file from the Task2 folder of the source code. The Designer tab of iReport shows a report containing data in Title, Column Header, Customer Group Header 1, Product Group Header 1, Detail 1, and Product Group Footer 1 sections, as shown in the following screenshot:

- Switch to the Preview tab and you will see invoices for each customer grouped by product names.

- Switch back to the Designer tab. Right-click on the Variables node in the Report Inspector window on the left side of your report. From the pop-up menu that appears, select the Add Variable option.

- A new variable named variable1 will be added at the end of the variables list.

- While variable1 is selected, find the Name property in the Properties window below the Palette of components and change its value to FirstRecordOfANewGroup. Now the name of the variable1 variable will change to FirstRecordOfANewGroup.
- Select the Variable Class property and change its value to java.lang.Integer.
- Select the Calculation property and change its value to Count.
- Select the Reset type property and change its value to Group.
- Select the Reset group property and change its value to Customer.

- Select the Variable Expression property and click the button beside it. A Variable Expression window with no default expression will open, as shown in the next screenshot:

- Select Variables in the first column of the lower-half of the Variable Expression window. Then double-click the FirstRecordOfANewGroup variable in the second column. A new expression $V{FirstRecordOfANewGroup} will appear in the Variable Expression window, as shown in the next screenshot. Press the OK button.

- Right-click on the Variables node in the Report Inspector window. A pop-up menu will appear. Select the Add Variable option.
- A new variable named variable1 will be added at the end of the variables list.
- While variable1 is selected, find the Name property in the Properties window below the Palette of components and change its value to TOC. Now the name of the variable1 variable will change to TOC.
- Select the Variable Class property and change its value to java.lang.String.
Sign up for a Packt account to see the rest of this article
Now that you've read a few articles, you might want to consider signing up for a Packt account. It takes a matter of seconds, will give you access to all the articles on PacktPub.com, and once you've signed up you'll be returned here to carry on reading your article.
Furthermore, you'll gain access to nine free ebooks, and be offered a free trial of PacktLib, Packt's online library. Simply enter your details here, or log in to your existing account.




Post new comment