You can place subreports in your master report in such a way that the look and feel of the master report resembles that of a multi-column report. Therefore, you can use subreports to design multi-column reports.
However, doing this may cause minor problems in certain scenarios. This recipe demonstrates how to use subreports in multi-column reports and resolve problems that occur while doing this.
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 to create a database named jasperdb7
and copy sample data for this recipe into the database.
You will be using three JRXML files in this recipe: SimulatingMultiColumnReport.jrxml, CustomerInvoicesSubreport.jrxml
, and MonthlyOrderHistorySubreport.jrxml
. You will find these files in the Task5
folder of the source code download of this chapter. The SimulatingMultiColumnReport.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 on your PC. I have hardcoded this complete path in the master report (SimulatingMultiColumnReport.jrxml)
.
SimulatingMultiColumnReport.jrxml
file from the C:JasperReportsCookBookSamples
folder on your PC. The Designer tab of iReport shows a report containing data in the Title, Column Header, and Customer Group Header 1 sections, as shown in the following screenshot: CustomerInvoicesSubreport.jrxml
file placed in the C:JasperReportsCookBookSamples
folder on your PC and press the Open button, as shown in the following screenshot. The browser dialog will disappear. CustomerName
parameter of CustomerInvoicesSubreport.jrxml
and allows you to map it to elements (that is Fields or Variables) of the main SimulatingMultiColumnReport.jrxml
report. Click the Expression field beside the CustomerName
parameter. A drop-down list will open, which shows fields and variables of the master report. Select the CustomerName
field from the list, as shown in the following screenshot. Press the Next button. CustomerInvoicesSubreport.jrxml)
repeats for each record in the Detail 1 section of the master report (that is, SimulatingMultiColumnReport.jrxml)
. variable1
will be added at the end of the variables list. variable1
is selected, find the Name property in the Properties window below the Palette and change its value to FirstRecordOfANewGroup
. Now the name of the variable1
variable will change to FirstRecordOfANewGroup
. java.lang.Integer
. Count
. Group
. Customer
, as shown in the following screenshot: $V{FirstRecordOfANewGroup} + 1
in the expression editor window, as shown in the following screenshot. Press the OK button. $V{FirstRecordOfANewGroup} == 0
as value in the Print When Expression window, as shown in the following screenshot. Press the OK button to dismiss the window. MonthlyOrderHistorySubreport.jrxml
file located in the C:JasperReportsCookBookSamples
folder on your PC, and press the Open button, as shown in the following screenshot: CustomerName
parameter of MonthlyOrderHistorySubreport.jrxml
and allows you to map it to elements (that is, Fields or Variables) of the main SimulatingMultiColumnReport.jrxml
report. Click on the Expression field beside the CustomerName
parameter. A drop-down list will open, which shows the fields and variables of the master report. Select the CustomerName field from the list, as shown in the following screenshot. Click the Next button. $V{FirstRecordOfANewGroup} == 0
as value in the Print When Expression window, as shown in the following screenshot. Press the OK button.In this recipe, you have used two subreports in a master report. You placed the two subreports in such a way that the master report looks like a multi-column report.
Notice from the recipe that when you inserted the first subreport in step 3 of the recipe, it started repeating itself for every record. This is a common problem that you may encounter when you insert a subreport in any detail section of a master report.
In order to stop the subreport from repeating itself, you configured a variable named FirstRecordOfANewGroup
in steps 12 to 19. Then you configured the Print When Expression property of the two subreports in steps 22 and 33, which prints the subreports only on the first record of every customer, so that the subreports do not repeat for every record.
Also notice steps 20 and 31 of the recipe, where you configured the Remove Line When Blank property of the two subreports. This property removes the white space left empty when a subreport does not print due to the Print When Expression property.
You normally need to configure the Remove Line When Blank property whenever you wish to print a subreport conditionally. If you don't use this property, you will see a lot of unwanted white space in your report.
You can switch to the XML tab to see the following JRXML code:
<detail> <band height="28" splitType="Stretch"> <subreport> <reportElement x="6" y="4" width="305" height="16" isRemoveLineWhenBlank="true"> <printWhenExpression> <![CDATA[$V{FirstRecordOfANewGroup} == 0]]> </printWhenExpression> </reportElement> <subreportParameter name="CustomerName"> <subreportParameterExpression> <![CDATA[$F{CustomerName}]]> </subreportParameterExpression> </subreportParameter> <connectionExpression> <![CDATA[$P{REPORT_CONNECTION}]]> </connectionExpression> <subreportExpression class="java.lang.String"> <![CDATA[$P{SUBREPORT_DIR} + "CustomerInvoicesSubreport.jasper"]]> </subreportExpression> </subreport> <subreport> <!—details of this subreport omitted for brevity--> </subreport> </band> </detail>
In this recipe, you dragged-and-dropped two subreport components and so iReport authored two<subreport>
tags as shown in the above JRXML code. In steps 3 to 10, iReport authored the first<subreport>
tag, while in steps 24 to 30 it authored the second<subreport>
tag.
The following is a mapping of recipe steps to JRXML code of the first<subreport>
tag:
<connectionExpression>
tag shown highlighted.<subreportParameter>
tag shown highlighted.<![CDATA[$P{SUBREPORT_DIR} + "CustomerInvoicesSubreport.jasper"]]>
CDATA section. isRemoveLineWhenBlank="true"
attribute in response to your action of step 20, where you changed the Remove Line When Blank property of the subreport
component. subreport
component and iReport authored the<printWhenExpression>
3.14.144.229