I need to include or exclude a subreport based on a condition. I'm using iReport to create JasperReports. 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.
R – Inclusion and exclusion of subreports (iReport-JasperReports)
ireportjasper-reports
Related Solutions
You can pass datasource via the built-in REPORT_DATA_SOURCE
parameter.
The example:
<subreport>
<reportElement x="261" y="25" width="200" height="100"/>
<dataSourceExpression><![CDATA[$P{REPORT_DATA_SOURCE}]]></dataSourceExpression>
<subreportExpression><![CDATA[$P{SUBREPORT_DIR} + "subreport.jasper"]]></subreportExpression>
</subreport>
You can create new instance of datasource based on variable, parameter or field.
The sample:
<variable name="HeadingsCollection" class="java.util.Collection" calculation="System">
<initialValueExpression><![CDATA[new java.util.ArrayList()]]></initialValueExpression>
</variable>
...
<subreport>
<reportElement x="0" y="0" width="515" height="20"/>
<subreportParameter name="ReportTitle">
<subreportParameterExpression><![CDATA[$P{ReportTitle}]]></subreportParameterExpression>
</subreportParameter>
<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($V{HeadingsCollection})]]></dataSourceExpression>
<subreportExpression class="java.lang.String"><![CDATA["HeadingsReport.jasper"]]></subreportExpression>
</subreport>
Another sample:
<field name="cast" class="java.util.Collection"/>
...
<subreport>
<reportElement positionType="Float" x="15" y="25" width="245" height="20" isRemoveLineWhenBlank="true" backcolor="#99CCFF"/>
<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{cast})]]></dataSourceExpression>
<subreportExpression class="java.lang.String"><![CDATA["JRMDbCastSubreport.jasper"]]></subreportExpression>
</subreport>
Or you can pass the datasource via the parameter:
<parameter name="SubreportDataSource" class="net.sf.jasperreports.engine.JRDataSource"/>
...
<subreport>
<reportElement positionType="Float" x="15" y="25" width="245" height="20" isRemoveLineWhenBlank="true"/>
<dataSourceExpression>$P{SubreportDataSource}</dataSourceExpression>
<subreportExpression class="java.lang.String"><![CDATA["Subreport.jasper"]]></subreportExpression>
</subreport>
Note: Using the same (with master report) datasource in subreport can cause the effect of loosing the first row in the subreport. You can read Why is the first record missing from my subreport? post for understanding how to avoid this issue.
You can use Conditional styles
for solving this issue.
The sample:
<style name="ZFieldStyle">
<conditionalStyle>
<conditionExpression><![CDATA[$F{Z} < $F{Y}]]></conditionExpression>
<style forecolor="#000000"/>
</conditionalStyle>
<conditionalStyle>
<conditionExpression><![CDATA[$F{Z}>$F{X}]]></conditionExpression>
<style forecolor="#FF0000"/>
</conditionalStyle>
</style>
...
<field name="X" class="java.lang.Integer"/>
<field name="Y" class="java.lang.Integer"/>
<field name="Z" class="java.lang.Integer"/>
...
<textField>
<reportElement style="ZFieldStyle" x="200" y="0" width="100" height="20"/>
<textElement/>
<textFieldExpression><![CDATA[$F{Z}]]></textFieldExpression>
</textField>
Best Answer
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:
where TOTAL_STATS is from the master report's database query. If non-zero we show a subreport detailing some data.