JasperReports 3.6: Creating a Simple, One-page TOC for Your Report

Exclusive offer: get 50% off this eBook here
JasperReports 3.6 Development Cookbook

JasperReports 3.6 Development Cookbook — Save 50%

Over 50 recipes to create next-generation reports using JasperReports

$29.99    $15.00
by Bilal Siddiqui | June 2010 | Cookbooks Java Open Source

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...

  1. 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:

    JasperReports 3.6

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

    JasperReports 3.6

  3. 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.

    JasperReports 3.6

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

    JasperReports 3.6

  5. 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.
  6. Select the Variable Class property and change its value to java.lang.Integer.
  7. Select the Calculation property and change its value to Count.
  8. Select the Reset type property and change its value to Group.
  9. Select the Reset group property and change its value to Customer.

    JasperReports 3.6

  10. 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:
    JasperReports 3.6
  11. 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.

    JasperReports 3.6

  12. Right-click on the Variables node in the Report Inspector window. A pop-up menu will appear. Select the Add Variable option.
  13. A new variable named variable1 will be added at the end of the variables list.
  14. 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.
  15. Select the Variable Class property and change its value to java.lang.String.
JasperReports 3.6 Development Cookbook Over 50 recipes to create next-generation reports using JasperReports
Published: July 2010
eBook Price: $29.99
Book Price: $49.99
See more
Select your format and quantity:

(For more resources on JasperReports, see here.)

  1. Select the Reset type property and change its value to Report, as shown in the following screenshot:

    JasperReports 3.6

  2. Look for the Variable Expression property and click the button beside it. A Variable Expression window with no default expression will open.

    JasperReports 3.6

  3. Type ($V{FirstRecordOfANewGroup}==0) ? ($V{TOC}+$F{CustomerName} +" ............................................ " + $V{PAGE_NUMBER}+"<br/>") :$V{TOC} in the expression editor window, as shown in the next screenshot. Press the OK button.

    JasperReports 3.6

  4. Click the Initial Value Expression property and type new java.lang.String() as its value. Now the properties of the TOC variable are all set.
  5. Save your report by choosing the Save option from the File menu. Click the close icon to the right of your report title to close the report, as indicated in the following screenshot.

    JasperReports 3.6

  6. Re-open the saved SimpleTOCReport.jrxml file. You have to close and re-open your .jrxml file every time you play with the Initial Value Expression property of a variable. This is due to a bug in iReport version 3.6.0
  7. Click the Title section of your report. Its properties will appear in the Properties window below the Palette. Look for the Band height property and set its value as 200.
  8. Double-click the Variables node in the Report Inspector window on the left side of your report. The Variables node will expand showing all its children (that is, all variables present in the report) as shown in the following screenshot:

    JasperReports 3.6

  9. Drag-and-drop the variable named TOC from the Variables node into the Title section of your report, as shown in the next screenshot:

    JasperReports 3.6

  10. Click the $V{TOC} text field in the Title section. Its properties will appear in the Properties window below the Palette. Find the Width property and set its value as 300. Similarly, set 100 as the value of its Height property.
  11. Click the Markup property under the Text properties section in the Properties window and select html as its value from the drop-down list beside it.
  12. Switch to the Preview tab. You will see that the TOC field does not display any content, as shown in the following screenshot:

    JasperReports 3.6

  13. Switch back to the Designer tab. Click the $V{TOC} text field in the Title section. Its properties will appear in the Properties window below the Palette. Find the Evaluation Time property and select Report as its value from the drop-down list beside it.

    JasperReports 3.6

  14. Drag-and-drop a Break component from the Palette into the Title section below the $V{TOC} text field. You will see a prompt to choose between a Page break or Column break, as shown in the next screenshot. Press OK to accept the Page break option, selected by default.

    JasperReports 3.6

  15. Switch to the Preview tab. You will see that the TOC of your report is displayed below the title and the actual report starts on a new page due to the page break you inserted after the TOC.

    JasperReports 3.6

    1. It checks whether it is the start of a new customer.
    2. If it is the start of a new customer, the TOC variable concatenates the customer name and current page number into its current value.
    3. If it is not the start of a new customer, the TOC variable simply holds its previous contents without any change.
  16. How it works...

    You have used a two-variable trick to build a one-page TOC for your customer invoices report. The two variables are FirstRecordOfANewGroup (which you named in step 5) and TOC (which you named in step 14). These two variables together build the TOC, as explained below.

    The FirstRecordOfANewGroup variable keeps count of records for each customer. This variable resets at the start of records for each customer, because you set its Reset type property to Group and its Reset group property to Customer in steps 8 and 9, respectively. Moreover, the FirstRecordOfANewGroup variable is incremented by 1 for each record due to count being the value of the Calculation property you set in step 7. Therefore, the value of this variable is zero (0) at the start of each customer and increments as JasperReports processes records of that customer.

    Now look at the ($V{FirstRecordOfANewGroup}==0 ) ? ($V{TOC} + $F{CustomerName} + " ............................................" + $V{PAGE_NUMBER}+"<br/>") :$V{TOC} expression for the TOC variable that you authored in step 18. This is a complex expression which does the following:

    This way, the TOC variable builds the complete TOC of the report. Note that you dropped the TOC variable as a field into the Title section in step 24 and set the Evaluation Time property of the $V{TOC} field to Report. This means that the value of the TOC variable will be copied to the $V{TOC} field only at the end of the report processing, so that the complete TOC is displayed.

    Similarly, in step 26 you selected html as the value of the Markup property of the $V{TOC} field because you used a < /br> HTML tag in the expression for the TOC variable earlier in step 18.

    Summary

    This article illustrated the method to build a simple one-page table of contents(TOC) for your multi-page reports.


    Further resources on this subject:


JasperReports 3.6 Development Cookbook Over 50 recipes to create next-generation reports using JasperReports
Published: July 2010
eBook Price: $29.99
Book Price: $49.99
See more
Select your format and quantity:

About the Author :


Bilal Siddiqui

Bilal Siddiqui is an Electronics Engineer, an XML consultant, and the founder of XML4Java.com, a company focused on simplifying e-business. After graduating in 1995 with a degree in electronics engineering from the University of Engineering and Technology Lahore, he began designing software solutions for industrial control systems. Later, he turned to XML and used his programming experience in C++ to build web- and WAP-based XML processing tools, server-side parsing solutions, and service applications. Bilal is a technology evangelist and a frequently published technical author.

Bilal has been focusing exclusively on Java and XML-based open source tools and solutions since 2006. He has extensively used popular open source products such as JasperReports, ADempiere, Openbravo, and Eclipse. Bilal is a strong advocate of open source tools and is engaged not only in designing solutions based on open source tools but also collaborating with local universities in Lahore to train software and IT personnel in using open source technologies.

Books From Packt


iReport 3.7
iReport 3.7

Pentaho Reporting 3.5 for Java Developers
Pentaho Reporting 3.5 for Java Developers

Amazon SimpleDB Developer Guide
Amazon SimpleDB Developer Guide

RESTful Java Web Services
RESTful Java Web Services

Apache JMeter
Apache JMeter

Apache Geronimo 2.1: Quick Reference
Apache Geronimo 2.1: Quick Reference

Tomcat 6 Developer's Guide
Tomcat 6 Developer's Guide

JavaFX 1.2 Application Development Cookbook
JavaFX 1.2 Application Development Cookbook


Code Download and Errata
Packt Anytime, Anywhere
Register Books
Print Upgrades
eBook Downloads
Video Support
Contact Us
Awards Voting Nominations Previous Winners
Judges Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
Resources
Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software