Search code examples
jasper-reportsis-empty

Jasper Reports - check if table is empty (no records) and hide table label


My scenario: 2 tables on Summary field, each table has its own dataset. Also, each table is labelled so there is a label just on top each table.All is ok, however if there is no data in table, table is not printed but table label is still visible.

I need to hide table label if table has no data. Is it even possible? I can't find any solution how to determine if table is empty or not, to set is as expression that will hide label.

Any ideas?


Solution

  • There is no problem to get the record count from your table component this is done by returning value

    <returnValue fromVariable="REPORT_COUNT" toVariable="TABLE_COUNT"/>
    

    However this will not solve you problem since you can not set the evalutationTime of the printWhenExpression, you can only set evalutationTime of the textFieldExpression

    The problem is normally solved by moving your "table label" where it is expected to be, in the table header

    From IDE group your columns and add the textField in the groupHeader inside the table header.

    Example (jrxml result)

    <componentElement>
        <reportElement key="table" style="table" x="0" y="33" width="360" height="50" uuid="6a7d5ab9-f15d-4676-85b2-1e48f016c155"/>
        <jr:table xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd">
            <datasetRun subDataset="tableData" uuid="fa5df3de-f4c5-4bfc-8274-bd064e8b81e6">
                <connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
            </datasetRun>
            <jr:columnGroup width="217" uuid="c96f6ab0-8cce-4dc7-b686-da6928e7cabb">
                <jr:tableHeader height="61" rowSpan="1">
                    <textField>
                        <reportElement x="0" y="0" width="217" height="61" uuid="98aa260d-7e84-4df7-89cc-fc1eabdcf656"/>
                        <textElement textAlignment="Center" verticalAlignment="Middle"/>
                        <textFieldExpression><![CDATA["MY TABLE LABEL"]]></textFieldExpression>
                    </textField>
                </jr:tableHeader>
                <jr:column width="90" uuid="889a828a-ad48-40a0-81f4-326a95d6585c">
                    <jr:columnHeader style="table_CH" height="60" rowSpan="1"/>
                    <jr:detailCell style="table_TD" height="43" rowSpan="1">
                        <textField>
                            <reportElement x="0" y="0" width="90" height="43" uuid="82b2a835-4373-4638-b75a-d43f531678ba"/>
                            <textFieldExpression><![CDATA[$F{myField1}]]></textFieldExpression>
                        </textField>
                    </jr:detailCell>
                </jr:column>
                <jr:column width="127" uuid="004c3afe-c39f-44e2-a82d-5c96697ebd7e">
                    <jr:columnHeader style="table_CH" height="60" rowSpan="1"/>
                    <jr:detailCell style="table_TD" height="43" rowSpan="1">
                        <textField>
                            <reportElement x="0" y="0" width="90" height="43" uuid="90287319-765f-49a0-9f80-0851f40f2b13"/>
                            <textFieldExpression><![CDATA[$F{myField2}]]></textFieldExpression>
                        </textField>
                    </jr:detailCell>
                </jr:column>
            </jr:columnGroup>
        </jr:table>
    </componentElement>