iReport Ultimate Guide - Nimsoft Library

iReport Ultimate Guide - Nimsoft Library iReport Ultimate Guide - Nimsoft Library

13.07.2015 Views

iReport Ultimate GuideFigure 10-20 The Subreport ExpressionIf you choose the second option, the expression will store the entire absolute path to the subreport Jasper file. This insures thateverything will work in iReport, but it is not very convenient in other environments. For this reason, I suggest that you modifythe expression using the method I described in 10.1.2, “Specifying the Subreport,” on page 153.The subreport is not compiled when you create it. To test your report, you must first preview it. This forces it to be compiled.When you create a new subreport, you can’t specify parameters using the wizard. You will be able to add parameters and usethem in the subreport query after the report is created. So at this point, to filter your subreport query follow these steps:1. Add a parameter to the report implementing your subreport.2. Use that parameter in your query with the typical syntax $P{MyParam} (or $!P{MyParam} if the parameter must beconcatenated with the query as-is).3. In the master report, select the subreport element and add an entry in the subreport parameters list. The new subreportparameter must be called, like the counterpart in the subreport, and its value must be defined using an expression (see thesection 10.1.4, “Passing Parameters,” on page 154” for details).4. If your subreport is not based on an SQL or HQL query, you must still set the subreport data source expression tosuccessfully run your report.5. As mentioned earlier, you can use the Subreport Wizard to create a brand new report that will be referenced as subreportor to refer to an existing report. In the latter case, if the chosen report contains one or more parameters, the wizardprovides an easy way to define a value for each one.10.4.2 Specifying an Existing Report in the Subreport WizardYou can point to an existing report as a subreport with the Subreport Wizard. The first step is to select a JRXML or a Jasperfile in the first screen of the wizard.The second step of the wizard manages expressions for the connection or data source used to fill the subreport (Figure 10-21).166

SubreportsFigure 10-21 Subreport connection setupTo select a connection:• You can select Use the same connection used to fill the master report when using a JDBC-based subreport. The JDBCconnection is passed to the subreport to execute it.• To specify a different JDBC connection, select Use another connection.• To use a JRDataSource object to fill the subreport, select Use a JRDataSource expression and write an expressioncapable of returning a JRDataSource object.• Select Use an empty datasource to set the data source expression to new JREmptyDataSource(). That creates aspecial data source that provides (when declared in this way) a single record with all the field values set to null. This isvery useful when the subreport is used to display static content.In some cases you may want to avoid using any connection or data source, such as when you are displaying static content.Usually a data source or a connection is always required to prevent the subreport being blank. When a subreport does notrequire a data source, though, it is implied that the report property When no data type is set to All Data NoDetails or No Data Section to ensure that at least a portion of the document is actually printed.If the selected report contains parameters, they are listed next (Figure 10-22). For each parameter, you can set a value bychoosing an object from the combo boxes. Of course, you can write your own expression, but no expression editor is providedin this context.167

SubreportsFigure 10-21 Subreport connection setupTo select a connection:• You can select Use the same connection used to fill the master report when using a JDBC-based subreport. The JDBCconnection is passed to the subreport to execute it.• To specify a different JDBC connection, select Use another connection.• To use a JRDataSource object to fill the subreport, select Use a JRDataSource expression and write an expressioncapable of returning a JRDataSource object.• Select Use an empty datasource to set the data source expression to new JREmptyDataSource(). That creates aspecial data source that provides (when declared in this way) a single record with all the field values set to null. This isvery useful when the subreport is used to display static content.In some cases you may want to avoid using any connection or data source, such as when you are displaying static content.Usually a data source or a connection is always required to prevent the subreport being blank. When a subreport does notrequire a data source, though, it is implied that the report property When no data type is set to All Data NoDetails or No Data Section to ensure that at least a portion of the document is actually printed.If the selected report contains parameters, they are listed next (Figure 10-22). For each parameter, you can set a value bychoosing an object from the combo boxes. Of course, you can write your own expression, but no expression editor is providedin this context.167

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!