Mateus Viccari
Mateus Viccari

Reputation: 7709

How to render a report band of a jasper report only when a subreport inside the band is not empty?

I have a main report which queries a list of items from the database in the detail band, and another detail band which contains a sub report that renders once for every record. I want this second detail band to be rendered only when the sub report within it is not empty.

I tried to get the REPORT_COUNT from the sub report into a return variable and set the Print when expression parameter as $V{SUBREPORT_ITEMS_ROW_COUNT} != null && $V{SUBREPORT_ITEMS_ROW_COUNT} > 0 but it looks like the return variable is computed after the band is rendered, so it can't see the variable even it being returned correctly.

What else can i do?

UPDATE Here is the main report's JRXML:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.1.0.final using JasperReports Library version 6.1.0  -->
<!-- 2016-01-21T10:03:49 -->
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="PlanoDeContasDetalhado" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="9fa6ae6c-0db5-4324-a65d-f0d61a8f30bb">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="SCA Banco de dados bagual"/>
    <style name="NIVEL">
        <conditionalStyle>
            <conditionExpression><![CDATA[$F{PLCTA15COD}.trim().length() == 1]]></conditionExpression>
            <style mode="Opaque" backcolor="#CFCFCF" fontSize="15" isBold="true" isItalic="true"/>
        </conditionalStyle>
    </style>
    <parameter name="EMPRICOD" class="java.lang.Integer"/>
    <parameter name="ID_SETOR" class="java.lang.Integer"/>
    <parameter name="CAMPO_DATA" class="java.lang.String"/>
    <parameter name="DATA1" class="java.util.Date"/>
    <parameter name="DATA2" class="java.util.Date"/>
    <parameter name="SUBREPORT_DIR" class="java.lang.String" isForPrompting="false">
        <defaultValueExpression><![CDATA["C:\\Users\\Mateus\\Documents\\NetBeansProjects\\AtualyRelatorios\\jasper\\"]]></defaultValueExpression>
    </parameter>
    <parameter name="CON_ITENS" class="java.sql.Connection" isForPrompting="false"/>
    <queryString>
        <![CDATA[select
plcta15cod,
plcta30codedit,
plcta60descr
from planodecontas
order by plcta15cod]]>
    </queryString>
    <field name="PLCTA15COD" class="java.lang.String"/>
    <field name="PLCTA30CODEDIT" class="java.lang.String"/>
    <field name="PLCTA60DESCR" class="java.lang.String"/>
    <title>
        <band height="82" splitType="Stretch">
            <staticText>
                <reportElement x="0" y="0" width="510" height="26" uuid="2fa3702f-ff66-41a6-81e9-7dbbbd791233"/>
                <textElement verticalAlignment="Bottom">
                    <font size="18" isBold="true"/>
                </textElement>
                <text><![CDATA[Detalhamento do plano de contas]]></text>
            </staticText>
            <staticText>
                <reportElement x="0" y="26" width="510" height="15" uuid="f23a5d3d-a05d-4e9c-9053-492e635cde26"/>
                <textElement>
                    <font size="11" isBold="false"/>
                </textElement>
                <text><![CDATA[Receitas & Despesas]]></text>
            </staticText>
        </band>
    </title>
    <detail>
        <band height="18" splitType="Stretch">
            <textField>
                <reportElement style="NIVEL" x="0" y="0" width="80" height="18" uuid="2db15faa-e12e-4fc3-a5ae-956d454e50da"/>
                <textElement verticalAlignment="Middle">
                    <font size="8"/>
                </textElement>
                <textFieldExpression><![CDATA[$F{PLCTA30CODEDIT}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement style="NIVEL" x="80" y="0" width="475" height="18" uuid="bbcbea9e-2b83-4cd5-aa1e-2daea32f33a0"/>
                <textElement verticalAlignment="Middle">
                    <font size="8"/>
                </textElement>
                <textFieldExpression><![CDATA[$F{PLCTA15COD}.trim().replaceAll(".","-")
+ $F{PLCTA15COD}.trim().replaceAll(".","-")
+ $F{PLCTA60DESCR}]]></textFieldExpression>
            </textField>
        </band>
        <band height="4">
            <subreport>
                <reportElement x="-20" y="0" width="595" height="4" uuid="572867ad-8bb8-4664-9ae9-a1120c102430"/>
                <subreportParameter name="EMPRICOD">
                    <subreportParameterExpression><![CDATA[$P{EMPRICOD}]]></subreportParameterExpression>
                </subreportParameter>
                <subreportParameter name="ID_SETOR">
                    <subreportParameterExpression><![CDATA[$P{ID_SETOR}]]></subreportParameterExpression>
                </subreportParameter>
                <subreportParameter name="CAMPO_DATA">
                    <subreportParameterExpression><![CDATA[$P{CAMPO_DATA}]]></subreportParameterExpression>
                </subreportParameter>
                <subreportParameter name="DATA1">
                    <subreportParameterExpression><![CDATA[$P{DATA1}]]></subreportParameterExpression>
                </subreportParameter>
                <subreportParameter name="DATA2">
                    <subreportParameterExpression><![CDATA[$P{DATA2}]]></subreportParameterExpression>
                </subreportParameter>
                <subreportParameter name="PLCTA15COD">
                    <subreportParameterExpression><![CDATA[$F{PLCTA15COD}]]></subreportParameterExpression>
                </subreportParameter>
                <connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
                <subreportExpression><![CDATA[$P{SUBREPORT_DIR} + "PlanoDeContasDetalhadoItens.jasper"]]></subreportExpression>
            </subreport>
        </band>
    </detail>
</jasperReport>

Upvotes: 2

Views: 1826

Answers (1)

Mario
Mario

Reputation: 11

Are you trying to create a Master-Detail report? if yes (I don't know if I misunderstand your question, but that is my answer): I used to have the the subreport in the same band (detail) , and pass the master parameter to the subreport , into the subreport the query will use it, so if the master parameter doesnt come in the main query it wont show anything into the subreport. I hope this be usefull for you.

Upvotes: 1

Related Questions