0
votes

I have several subreports and all of these subreports are contained in separate bands.

For example:

...
<detail>
    <band height="500">
            <subreport>
                <reportElement isPrintRepeatedValues="false" x="-10" y="0" width="325" height="1" isRemoveLineWhenBlank="true" backcolor="#ffcc99"/>
                    <dataSourceExpression><![CDATA[$P{DepartmentASubReportData}]]></dataSourceExpression>
                    <subreportExpression class="net.sf.jasperreports.engine.JasperReport">
                                    <![CDATA[$P{DepartmentASubReport}]]></subreportExpression>
            </subreport>
            <break type="Page">
                <reportElement x="-10" y="1" width="325" height="1" key="element-1"/>
            </break>
    </band> 
    <band height="500"> 
            <subreport> 
                <reportElement isPrintRepeatedValues="false" x="-10" y="1" width="325" height="1" isRemoveLineWhenBlank="true" backcolor="#ffcc99"/>
                    <dataSourceExpression><![CDATA[$P{DepartmentBSubReportData}]]></dataSourceExpression>
                    <subreportExpression class="net.sf.jasperreports.engine.JasperReport">
                                    <![CDATA[$P{DepartmentBSubReport}]]></subreportExpression>
            </subreport>
            <break type="Page">
                <reportElement x="-10" y="1" width="325" height="1" key="element-2"/>
            </break>
    </band>
    <band height="500"> 
            <subreport> 
                <reportElement isPrintRepeatedValues="false" x="-10" y="1" width="325" height="1" isRemoveLineWhenBlank="true" backcolor="#ffcc99"/>
                    <dataSourceExpression><![CDATA[$P{DepartmentCSubReportData}]]></dataSourceExpression>
                    <subreportExpression class="net.sf.jasperreports.engine.JasperReport">
                                    <![CDATA[$P{DepartmentCSubReport}]]></subreportExpression>
            </subreport>
            <break type="Page">
                <reportElement x="-10" y="1" width="325" height="1" key="element-3"/>
            </break>
    </band>
    ...
... 

If the subreport doesn't contains elements, I would like to hide the element band.

I added the variable SUB_REPORT_ROW_CNT to the subreport and linked it to the variable REPORT_COUNT, which contains the number of rows in the report (in the subreport in this case).

<variable name="SUB_REPORT_ROW_CNT" class="java.lang.Integer" resetType="Report" calculation="Nothing">
    <variableExpression><![CDATA[$V{REPORT_COUNT}]]></variableExpression>
</variable>

In every band of the master report I added the following:

<band height="500"> 
        <printWhenExpression><![CDATA[$V{SUB_REPORT_ROW_CNT} != 0]]></printWhenExpression>          
        <subreport> 
            <reportElement isPrintRepeatedValues="false" x="-10" y="1" width="325" height="1" isRemoveLineWhenBlank="true" backcolor="#ffcc99"/>
                <dataSourceExpression><![CDATA[$P{DepartmentASubReportData}]]></dataSourceExpression>
                <returnValue subreportVariable="SUB_REPORT_ROW_CNT" toVariable="SUB_REPORT_ROW_CNT"/>
                <subreportExpression class="net.sf.jasperreports.engine.JasperReport">
                                <![CDATA[$P{DepartmentASubReport}]]></subreportExpression>
        </subreport>
        <break type="Page">
            <reportElement x="-10" y="1" width="325" height="1" key="element-2"/>
        </break>
</band>

But the expression $V{SUB_REPORT_ROW_CNT} != 0 is always evaluated as false.

And if I print the value of a variable by using the next block, I always get a null.

<textField>
    <reportElement x="8" y="40" width="540" height="18"  />
    <textElement textAlignment="Center" verticalAlignment="Middle"/>
    <textFieldExpression><![CDATA[$V{SUB_REPORT_ROW_CNT}]]></textFieldExpression>
</textField>

What could be the error?.. How to return values from subreport to hide empty bands?..

I would be very grateful for the information. Thanks to all.

1
Related way to remove subreport if no records stackoverflow.com/q/34682494/5292302Petter Friberg

1 Answers

0
votes

I would suggest to use your already existing parameters instead of using a variable, that makes it unnecessarily complicated

printWhenExpression of the band which you want to hide:

<band height="500"> 
    <printWhenExpression><![CDATA[$P{DepartmentASubReportData} != null]]></printWhenExpression>
</band>

or

<band height="500"> 
    <printWhenExpression><![CDATA[$P{DepartmentASubReport} != null]]></printWhenExpression>
</band>

Of course you need to pass this parameters with NULL as value if there are no entries for the subreport to hide the band. That works for me and is a really easy solution.