Arendelle
Arendelle

Reputation: 127

How to show a some text when Table component is empty

I have a main report who call a subreport. This subreport have a table using dataset.

I want to display a message when I have no record in my table. How to do this?

Upvotes: 2

Views: 4942

Answers (1)

Alex K
Alex K

Reputation: 22857

The "fake" staticText with text you want (No data text, for example) behind the Table component in cooperantion with removed whenNoDataType property of Table component should solve this task.

Sample

<?xml version="1.0" encoding="UTF-8"?>
<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="Blank_A4_27" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="95f68ce4-441a-425c-88fb-b48fe96fac6b">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
    <style name="Table_TH" mode="Opaque" backcolor="#F0F8FF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
            <topPen lineWidth="0.5" lineColor="#000000"/>
            <leftPen lineWidth="0.5" lineColor="#000000"/>
            <bottomPen lineWidth="0.5" lineColor="#000000"/>
            <rightPen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="Table_CH" mode="Opaque" backcolor="#BFE1FF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
            <topPen lineWidth="0.5" lineColor="#000000"/>
            <leftPen lineWidth="0.5" lineColor="#000000"/>
            <bottomPen lineWidth="0.5" lineColor="#000000"/>
            <rightPen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="Table_TD" mode="Opaque" backcolor="#FFFFFF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
            <topPen lineWidth="0.5" lineColor="#000000"/>
            <leftPen lineWidth="0.5" lineColor="#000000"/>
            <bottomPen lineWidth="0.5" lineColor="#000000"/>
            <rightPen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <subDataset name="Empty Dataset1" uuid="21916aaf-ceb1-456b-a45e-6c31863ac208"/>
    <queryString>
        <![CDATA[]]>
    </queryString>
    <title>
        <band height="252" splitType="Stretch">
            <staticText>
                <reportElement x="0" y="40" width="555" height="35" uuid="ff8a82ab-c00f-422a-a907-06d4021b87bd"/>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font fontName="SansSerif" size="18" isBold="true"/>
                </textElement>
                <text><![CDATA[No data]]></text>
            </staticText>
            <componentElement>
                <reportElement x="0" y="20" width="555" height="200" uuid="1a91c926-a553-4eab-bc04-938c1a962f9a">
                    <property name="com.jaspersoft.studio.layout" value="com.jaspersoft.studio.editor.layout.VerticalRowLayout"/>
                    <property name="com.jaspersoft.studio.table.style.table_header" value="Table_TH"/>
                    <property name="com.jaspersoft.studio.table.style.column_header" value="Table_CH"/>
                    <property name="com.jaspersoft.studio.table.style.detail" value="Table_TD"/>
                </reportElement>
                <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" whenNoDataType="AllSectionsNoDetail">
                    <datasetRun subDataset="Empty Dataset1" uuid="1834081e-7aa0-4719-a0ac-e077f259cc18">
                        <connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
                    </datasetRun>
                    <jr:column width="555" uuid="eb741b0d-640c-48d1-b653-03703ce9e27e">
                        <jr:tableHeader style="Table_TH" height="30"/>
                        <jr:tableFooter style="Table_TH" height="30"/>
                        <jr:columnHeader style="Table_CH" height="30"/>
                        <jr:columnFooter style="Table_CH" height="30"/>
                        <jr:detailCell style="Table_TD" height="30"/>
                    </jr:column>
                </jr:table>
            </componentElement>
        </band>
    </title>
</jasperReport>

In Jaspersoft Studio (JSS) the design looks like:

Design in Studio

Output result

If we applied "All Section No Detail" value for Table's dataset (whenNoDataType property):

  • in JSS this settings can be set with "When No Data Type" combobox:

GUI for setting property

  • or in jrxml
<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" whenNoDataType="AllSectionsNoDetail">

The result of showing empty Table will be:

Preview in JSS

In case deleting this property (whenNoDataType is NULL) the result will be:

Preview in JSS

in this case the Table tag at jrxml will be:

<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">

Upvotes: 6

Related Questions