Creating a simple, one-page TOC for your report

This recipe shows how you can build a simple one-page TOC for your report.

Getting ready

Refer to the 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. 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.

How to do it...

  1. Open the SimpleTOCReport.jrxml file from the Task2 folder of the source code of this chapter. 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:
    How to do it...
  2. Switch to the Preview tab and you will see invoices for each customer grouped by product names.
    How to do it...
  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.
    How to do it...
  4. A new variable named variable1 will be added at the end of the variables list.
    How to do it...
  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.
    How to do it...
  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:
    How to do it...
  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.
    How to do it...
  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.
  16. Select the Reset type property and change its value to Report, as shown in the following screenshot:
    How to do it...
  17. Look for the Variable Expression property and click the button beside it. A Variable Expression window with no default expression will open.
    How to do it...
  18. 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.
    How to do it...
  19. 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.
  20. 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.
    How to do it...
  21. 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
  22. 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.
  23. 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;
    How to do it...
  24. Drag-and-drop the variable named TOC from the Variables node into the Title section of your report, as shown in the next screenshot:
    How to do it...
  25. 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.
  26. 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.
  27. Switch to the Preview tab. You will see that the TOC field does not display any content, as shown in the following screenshot:
    How to do it...
  28. 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.
    How to do it...
  29. 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.
    How to do it...
  30. 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.
    How to do it...

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:

  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.

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.178.151