views:

4036

answers:

3

Just needed to include or exclude a subreport based on a condition when using IReport tool to create Jasper reports. I.e., if a subreport has values, I need to include that subreport, otherwise not. Can anyone please send a sample or tell me how to resolve this.

thanks in advance.

+1  A: 

you can in the master report get data from your data source that allows you to identify if the subreport should be included, then use the 'printWhenExpression' field on the subreport element to check that data.

I use this regularly - for example the printWhenExpression field may contain:

new Boolean($F{TOTAL_STATS}.intValue() != 0)

where TOTAL_STATS is from the master report's database query. If non-zero we show a subreport detailing some data.

Jamie Love
A: 

From my knowledge, if there aren't any data printed in the subreport, Jasper eliminates the blank space. In other words, if your subreport would normally occupy half a page, but there is no data, then the empty space is eliminated. I'm not sure this is what you mean by including or excluding the report.

Another solution would be to place the subreport in the main report, and from right-click ->properties -> Subreport tab, you select the "Subreport condition" tab and write your condition there. If the condition is true, the content of the subreport will be printed, otherwise it won't be printed.

Jenny Smith
A: 

Jamie Love's answer about "printWhenExpression" is on target.

if there aren't any data printed in the subreport, Jasper eliminates the blank space

Actually, at least in JasperReports 3.7.0 there is a subreport property "Remove Line When Blank" that controls that behavior. By default - at least when I use iReport - that parameter is off.

WEG

WEG