Remove text field if value in datasource is empty?

前端 未结 1 1093
长情又很酷
长情又很酷 2021-01-03 17:08

If I am creating a Jasper report template file in the iReports designer, is it possible to prevent a static text field from displaying if a field in its datasource is empty?

相关标签:
1条回答
  • 2021-01-03 17:54

    Is it possible to perhaps hide an element if a field value is NULL or empty?

    Yes, it is possible.

    1. Using "Print When Expression" property for static and text fields

    Sample for hiding NULL or "empty" string value:

            <staticText>
                <reportElement x="52" y="16" width="100" height="20">
                    <printWhenExpression><![CDATA[$F{field1} != null && $F{field1}.trim().length()>0]]></printWhenExpression>
                </reportElement>
                <textElement/>
                <text><![CDATA[Static text]]></text>
            </staticText>
    
    
            <textField>
                <reportElement x="170" y="15" width="100" height="20">
                    <printWhenExpression><![CDATA[$F{field2} != null && $F{field2}.trim().length()>0]]></printWhenExpression>
                </reportElement>
                <textElement/>
                <textFieldExpression><![CDATA[$F{field2}]]></textFieldExpression>
            </textField>
    

    2. Using "Blank When Null" property for text fields

    Sample for hiding text field with NULL value:

            <textField isBlankWhenNull="true">
                <reportElement x="340" y="15" width="100" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[$F{field3}]]></textFieldExpression>
            </textField>
    

    3. Using "No Data" band for empty datasource - no data returns

    If datasource is empty you can use "No Data" band with static fields you needs. For using this band you must set "When No Data" report's property to "No Data Section".

    Sample:

    <?xml version="1.0" encoding="UTF-8"?>
    <jasperReport .. whenNoDataType="NoDataSection" ..>
    
        ...
        <noData>
            <band height="50">
                <staticText>
                    <reportElement x="236" y="18" width="100" height="20"/>
                    <textElement/>
                    <text><![CDATA[No data]]></text>
                </staticText>
            </band>
        </noData>
    </jasperReport>
    
    0 讨论(0)
提交回复
热议问题