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

If you’re a Java developer who wants to make the most of the world’s most popular open source reporting engine, this book is all you need: a step-by-step course in creating super-attractive, user-friendly business 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.

(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:


Books to Consider

comments powered by Disqus