Subreports

The subreport element is used to include inside a report another report represented by an external Jasper file and feed using the same database connection used by the parent report or thought a data source that is specified in the subreport properties. Subreport Expression identifies the expression that returns a subreport expression class object at run time. According to the return type, the expression is evaluated in order to recover a Jasper object to be used to produce the subreport. In case the expression class is set to java.lang.String, JasperReports looks for a file following the same approach as Image Expression of the Image element.

Subreports are useful when your chart does not need all the same data as the main report.

The following briefly describes the characteristics of subreports:

<span class="Code">Subreport Expression </span>

This identifies the expression that returns a subreport expression class object at run time. According to the return type, the expression is evaluated in order to recover a Jasper object to be used to produce the subreport. In case the expression class is set to java.lang.String, JasperReports looks for a file following the same approach explained for the Image Expression of the Image element.

<span class="Code">Subreport Expression Class</span>

This is the class type of the expression; there are several options, each of one subtends to a different way to load the JasperReport object used to fill the subreport.

<span class="Code">Using cache </span>

This specifies whether to keep in memory the report object used to create the subreport in order to avoid reloading it all the times it is used inside the report. It is common that a subreport element placed, for instance, into the Detail band is printed more than once (or once for each record in the main dataset). The cache works only if the subreport expression is of type String since that string is used as key for the cache.

<span class="Code">Connection/Datasource Expression</span>

This identifies the expression that returns at run time a JDBC connection or a JRDataSource used to fill in the subreport. Alternatively the user can choose to avoid to pass any data. This last option is possible and many times it is very useful, but requires some expedient in order to make the subreport to work. Since a subreport (like a common report) returns an empty document if no data are provided, the subreport document should have the document property When No Data Type set to something like All Sections, No Detail.

<span class="Code">Parameters Map Expression</span>

This optional expression can be used to produce at run time an object of type java.util.Map. The map must be contain a set of coupled names/objects that are passed to the subreport in order to set a value for its parameters. Nothing disallows to use this expression in order to pass as parameters map to the subreport a map previously passed as parameter to the parent report.

<span class="Code">Subreport parameters </span>

This table allows you to define some coupled names/expressions that are useful for dynamically set a value for the subreport parameters by using calculated expressions.

<span class="Code">Subreport return values </span>

This table allows you to define how to store in local variables values calculated or processed in the subreport (such as totals and record count).

To create a subreport:

1. Drag the Subreport element from the Palette to the area of your report where you want to use it.

The Subreport wizard opens. You have three options:

Create a new report: Use this option when you need to use different data or a new query from your main report.
Select an existing report: Use this option when you already have a report using the data and query you need.
Just create the subreport element: Use this option to create a placeholder to be used later.

Subreport Wizard

2. Select Create a new report and click Next.

The New Report Wizard > Report Templates window appears.

3. Select a template for your subreport. For this example, select one of the blank templates. Click Next.

The New Report Wizard > Report file window opens.

4. Select a location for your subreport, and name it. Click Next.

The Data Source window opens.

Data Source and Query

5. In this step you can choose to use the same data adapter as the main report, or a different data adapter.

For this example, choose the same adapter (Sugar CRM). Enter the following SQL statement:

select count (*), shipcity from orders group by shipcity

Add all the fields to the list on the right. Click Next.

6. Click Next to skip the Group By step.

The Subreport > Connection window opens.

Subreport > Connection Window

7. In this window you have a choice of whether to connect to the same database as the main report or to a different database. For this example, click Use same connection used to fill the master report. Click Next.

The Subreport Parameters window opens.

Subreport Parameters Window

8. In this window you can add any parameters needed in your subreport. For this example, skip this window and click Finish.

A new report opens, containing all bands.

9. Delete all bands but the Title or Summary band, because those two bands only print once per report. Deleting the other bands eliminates extra white space in your report.

You now have a location into which to place your table, chart, or other element attached to the new subreport.

Version: 
Feedback
randomness