Print Empty table in iReport when associated query has no records

noble bhaskar picture noble bhaskar · Jan 2, 2013 · Viewed 6.9k times · Source

I am using iReports version 3.7.6. I am generating a table dynamically by associating it to a query using data set.

When the query returns records, I am getting a table printed containing all the records.

However when query returns zero records, I need to display an empty table. But I am getting just a horizontal line.

Below is the part of the jasper XML file:

<componentElement>
    <reportElement x="131" y="11" width="424" height="24"/>
    <jr:list xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd" printOrder="Vertical">
        <datasetRun subDataset="pmDataSet">
            <parametersMapExpression><![CDATA[$P{REPORT_PARAMETERS_MAP}]]></parametersMapExpression>
            <connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
        </datasetRun>
        <jr:listContents height="24" width="424">
            <textField>
                <reportElement x="0" y="0" width="134" height="22"/>
                <box>
                    <topPen lineWidth="0.5"/>
                    <rightPen lineWidth="0.5"/>
                </box>
                <textElement/>
                <textFieldExpression class="java.lang.String"><![CDATA[$F{COLUMN_1}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="134" y="0" width="120" height="22"/>
                <box>
                    <topPen lineWidth="0.5"/>
                    <rightPen lineWidth="0.5"/>
                </box>
                <textElement/>
                <textFieldExpression class="java.lang.String"><![CDATA[($F{COLUMN_2}) ? $F{COLUMN_2} : "(Blank)"]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="254" y="0" width="170" height="22"/>
                <box>
                    <topPen lineWidth="0.5"/>
                    <rightPen lineWidth="0.5"/>
                </box>
                <textElement/>
                <textFieldExpression class="java.lang.String"><![CDATA[$F{COLUMN_3}]]></textFieldExpression>
            </textField>
        </jr:listContents>
    </jr:list>
</componentElement>

Here I have used "List" element (jr:list) and associated it with a data set.

Answer

Gopinagh.R picture Gopinagh.R · Jan 5, 2013

As far as i know, this cannot be achieved with a table. But still, there is a work around.

Use a Subreport instead of a table. Everything that is accomplished using table can also be done using a subreport. Only thing you will need to take care of is this property.

When No Data

Set it either to

  • All Sections No Detail : display only header information
  • No Data Section: so that a custom message can be displayed in the No Data band.

Hope this helps.