Managing pagination of multiple types of data in a report

JasperReports allows full flexibility of managing and interlinking of pagination for data spanning multiple subreports. The subreports may contain different types of data and may serve as building blocks for a single master report.

In this recipe you will learn how to make complex pagination for multiple types of data reports and interlinking pagination of each datatype using built-in features of JasperReports.

This recipe also teaches you how to use data-specific and general pagination in parallel with each other. Data-specific pagination is contributed by each building block (subreport), while the general pagination is contributed by the master 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 two files named copySampleDataIntoPGS.txt and copyCustomerHistorySampletDataIntoPGS.txt. The copySampleDataIntoPGS.txt file will help you create a database named jasperdb6 and create a table named CustomerInvoices with five columns (InvoiceID, CustomerName, InvoicePeriod, ProductName, and InvoiceValue) and copy sample data for this recipe. Similarly, the copyCustomerHistorySampletDataIntoPGS.txt file will help you create a database named jasperdb6a and create a table named CustomerHistory with five columns (InvoiceID, CustomerName, InvoicePeriod, ProductName, and InvoiceValue) and copy sample data.

You will be using three JRXML files MultipleData.jrxml, CustHistorySubreport.jrxml and ProductInvoices.jrxml in this recipe. You will find these files in the Task6 folder of the source code download of this chapter. The MultipleData.jrxml file is the master report, which uses the other two files as subreports. The master report has to refer to all its subreports using a complete path (you cannot use relative paths). This means you have to copy the three JRXML files to the C:JasperReportsCookBookSamples folder in your PC. I have hard-coded this complete path in the master report (MultipleData.jrxml).

How to do it...

  1. Open the MultipleData.jrxml file from the C:JasperReportsCookBookSamples folder in your PC. The Designer tab of iReport shows a report with data in the Title, Customer Group Header 1, Detail1, and Detail2 sections, as shown in the following screenshot:
    How to do it...
  2. Switch to the Preview tab and you will see a report with 12-months order history and invoices for a number of customers.
    How to do it...
  3. Switch back to the Designer tab. Drag-and-drop a Break component from the Palette into the Detail1 section of your report. A dialog will appear. Select the Page break option in the dialog and press the OK button, as shown below:
    How to do it...
  4. Select the Page break component you just dropped and right-click on it. A pop-up menu will appear. Select Align from the pop-up menu, and a sub pop-up menu will appear. Select Align To Bottom Margin from the sub pop-up menu.
    How to do it...
  5. The Page break component will be positioned exactly below the Subreport component in the Detail1 section.
    How to do it...
  6. Select the Subreport component in the Detail1 section and right-click on it. A pop-up menu will appear. Left-click on Open Subreport in the pop-up menu, as shown in the next screenshot:
    How to do it...
  7. A file named CustHistorySubreport.jrxml will open in a new tab. Drag-and-drop the Page number component from the Palette into the Page Footer section of the CustHistorySubreport.jrxml subreport. A Text field with the expression $V{PAGE_NUMBER} will appear in the Page Footer section of the CustHistorySubreport.jrxml report.
    How to do it...
  8. In the Report Inspector select the Page Footer section and change the Band height property in the Page Footer - Properties section to 80. This will increase available space in the Page Footer section.
    How to do it...
  9. Increase the size of the Text field with expression $V{PAGE_NUMBER} in the Page Footer section by dragging its left edge to the left-side and the right edge to the right side. Double-click the Text field and its expression will be selected. Press the Home key on your keyboard and type"Page +. Then press the End key on your keyboard and type +" of + $F{CustomerName}. After typing, click anywhere outside the Text field. The expression will become"Page "+ $V{PAGE_NUMBER} + of + $F{CustomerName}.
    How to do it...
  10. Select this Text field of the Page Footer section and change the Expression Class property in the Properties section to java.lang.String.
    How to do it...
  11. Save and close the CustHistorySubreport.jrxml subreport. Switch to the Preview tab of the master report, and you will see Page1 of Alice appearing at the bottom, as shown below. Check all the other pages as well. You will notice that only the first page of each customer is showing pagination; the subsequent pages don't carry any pagination.
    How to do it...
  12. Switch back to the Designer tab. Select the Subreport component in the Detail2 section and right-click on it. A pop-up menu will appear. Left-click on Open Subreport in the pop-up menu.
  13. A file named ProductInvoices.jrxml will open in a new tab. In the Report Inspector, select the Page Footer section and change the Band height property in the Page Footer-Properties section to 80. This will increase available space in the Page Footer section.
  14. Drag-and-drop the Page number component from the Palette into the Page Footer section of the ProductInvoices.jrxml report. A Text Field with the expression $V{PAGE_NUMBER} will appear in the Page Footer section of ProductInvoices.jrxml report, as shown below.
    How to do it...
  15. Increase the size of the Text Field with the expression $V{PAGE_NUMBER} in the Page Footer section by dragging its left edge to the left side and its right edge to the right side. Double-click the Text Field and the expression will be selected. Press the Home key on your keyboard and type"Page " + ( . Then press the End key on your keyboard and type + 1 ) + " of " + $F{CustomerName}. After typing, click anywhere outside the Text Field. The expression will become"Page "+ ($V{PAGE_NUMBER}+1) + " of " + $F{CustomerName}, as shown below:
    How to do it...
  16. Select this Text Field that you just edited in the Page Footer section and change the Expression Class property from the Properties section to java.lang.String.
  17. Save and close the ProductInvoices.jrxml subreport. Switch to the Preview tab of the master report, and you will see Page 1 of Alice is appearing at the bottom of page 1 and Page 2 of Alice is appearing at the bottom of page 2, as shown in the following screenshot. Check all the other pages as well. You will notice that each page of each customer is showing the correct pagination.
    How to do it...
  18. Switch back to the Designer tab. Drag-and-drop the Page X of Y component from the Palette into the Page Footer section of the master report. Two text fields with expressions"Page "+$V{PAGE_NUMBER}+" of" and" " + $V{PAGE_NUMBER} respectively will appear in the Page Footer section.
    How to do it...
  19. Switch to the Preview tab on the master report, and you will see that Page 1 of 9 is appearing at the bottom of page 1 and Page 2 of 9 is appearing at the bottom of page 2, and so on. Check all the other pages as well. You will notice that each page is now showing the correct pagination.

How it works...

In step 3 of the recipe you inserted a page break after CustHistorySubreport.jrxml subreport. Due to this page break, CustHistorySubreport.jrxml subreport, which is a single-page report, will display its data on a separate page. The ProductInvoices.jrxml subreport will fill multiple pages for each customer.

If each subreport contributes its natural pagination to the master report, the master will have Page 1 at two places (that is, the first page of each subreport). You corrected this problem by incrementing to the page numbers of the ProductInvoices.jrxml subreport in step 15 of this recipe by using the expression ($V{PAGE_NUMBER}+1).

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

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