Applying a style to your simple TOC

This recipe teaches you how to play with variables to give a stylish and professional look to the simple Table of contents (TOC) you designed in the Creating a simple one-page TOC for your report recipe. For this purpose, you will use four pairs of variables and text fields to design a stylish TOC. You will configure each pair individually.

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 StylishTOCReport.jrxml file from the Task3 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 a simple TOC on the first page of your report.
    How to do it...
  3. Switch back to the Designer tab. 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 next screenshot.
    How to do it...
  4. Select the variable named TOC, and its Properties window will appear below the Palette of components. Select the Variable Expression property and click the button beside it. A Variable Expression window will open.
    How to do it...
  5. Delete the existing expression and type ($V{FirstRecordOfANewGroup}==0) ? $V{TOC} + ($F{CustomerName} +"<br/>") :$V{TOC} as the expression in the Variable Expression window.
    How to do it...
  6. Switch to the Preview tab and you will see that the dotted lines and page numbers in the TOC are gone, as shown in the following screenshot:
    How to do it...
  7. Switch back to the Designer tab. Right-click on the Variables node in the Report Inspector window. A pop-up menu will appear. Select the Add Variable option.
  8. A new variable named variable1 will be added at the end of the variables list.
  9. While variable1 is selected, find the Name property in the Properties window below the Palette of components and change its value to TOC_PageIndex. Now the name of the variable1 variable will change to TOC_PageIndex.
  10. Select the Variable Class property and change its value to java.lang.String.
  11. Select the Reset type property and change its value to Report.
  12. 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 following screenshot:
    How to do it...
  13. Type ($V{FirstRecordOfANewGroup}==0) ? ($V{TOC_PageIndex}+$V{PAGE_NUMBER} + "<br/>") :$V{TOC_PageIndex} as the expression in the Variable Expression window, as shown below. Press the OK button.
    How to do it...
  14. Select the Initial Value Expression property and type new java.lang.String() as its value.
    How to do it...
  15. Save your report by choosing Save option from the File menu. Click the close icon on right of your report title to close the report, as indicated in the next screenshot:
    How to do it...
  16. Re-open the saved StylishTOCReport.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.
  17. Double-click the Variables node in the Report Inspector window; it will show all its children (that is, all variables present in the report). Drag-and-drop, TOC_PageIndex variable from the Variables node to the far right of the Title section beside the $V{TOC} text field of your report, as shown in the next screenshot:
    How to do it...
  18. Click the $V{TOC_PageIndex} text field in the Title section. Its properties will appear in the Properties window below the Palette. Find the Height property and set its value as 100.
  19. Find the Evaluation Time property of the $V{TOC_PageIndex} text field and select Report as its value from the drop-down list beside it. Similarly, look for the Markup property and select html as its value from the drop-down list beside it.
  20. Switch to the Preview tab. You will see the TOC now showing customer names as well as page numbers.
    How to do it...
  21. Switch back to the Designer tab. Right-click on the Variables node in the Report Inspector window. A pop-up menu will appear. Select the Add Variable option.
  22. A new variable named variable1 will be added to the end of the variables list.
  23. While variable1 is selected, find the Name property in the Properties window below the Palette of components and change its value to TOC_Line. Now the name of the variable1 variable will change to TOC_Line.
  24. Click the Initial Value Expression property and type new java.lang.String() as its value.
  25. Select the Variable Expression property and click the button beside it. A Variable Expression window with no default expression will open.
  26. Type ($V{FirstRecordOfANewGroup} == 0) ? ($V{TOC_Index} + $V{Group_Count}+".<br/>") :$V{TOC_Index} as the Variable Expression value in the expression editor window.
    How to do it...
  27. Repeat steps 15 and 16 to close and re-open the StylishTOCReport.jrxml file.
  28. Drag-and-drop the TOC_Index variable from the Variables node into the Title section just to the left beside the $V{TOC_Index} text field, as the shown in the next screenshot:
    How to do it...
  29. While the $V{TOC_Index} text field is selected, find the Width property and set its value as 30. Similarly, set 100 as the value of the Height property.
  30. Find the Evaluation Time property in the Properties window under the Text field properties section and select Report as its value from the drop-down list beside it.
  31. Find 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.
  32. Now adjust the placement of all the four text fields in the Title section close to each other by using mouse or arrow keys. Finally, the Title section will look as shown in the following screenshot:
    How to do it...
  33. Click on each of the four text fields one by one and set 12 as the value of the Size property in the Properties window.
  34. Switch to the Preview tab, you will see that the TOC is showing customer names and page numbers, as shown in the next screenshot:
    How to do it...

How it works...

In this recipe you learned a trick of using pairs of variables and text fields to produce a stylish TOC. The recipe uses four pairs:

  1. A variable named TOC and its corresponding $V{TOC} text field form the first pair, which was already present in the StylishTOCReport.jrxml file when you opened it in step 1. This pair displays the customer names. Refer to the earlier recipe Creating a simple one-page TOC for your report of this chapter to learn how this pair works.
  2. A variable named TOC_PageIndex and its corresponding $V{TOC_PageIndex} text field form the second pair, which you configured in steps 7 to 18. This pair displays all the page numbers.
  3. A variable named TOC_Line and its corresponding $V{TOC_Line} text field form the third pair, which you configured in steps 21 to 32. This pair displays the dotted line between customer names and page numbers.
  4. The last pair of variable named TOC_Index and its corresponding $V{TOC_Index} text field form the fourth pair, which you configured in steps 34 to 51. This pair uses a supporting variable named Group_Count, which holds the count for the customer names, to display incremental index numbers for the TOC.

The common theme between all the pairs of variables and text fields is the use of string concatenation. The variable of each pair keeps on building its content or value for each customer. At the end of report processing, the variable simply copies its value into its corresponding text field. As you want the variable to copy its value into the field only at the end of report processing, you set Report as the value of the Evaluation Time property for all text fields. Similarly, because each variable uses a<br/> HTML tag in its expression, you have to set html as the value of its Markup property.

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

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