Remove text field if value in datasource is empty?
If I am creating a Jasper report template file in the iReports designer, is it possibl开发者_如何学运维e to prevent a static text field from displaying if a field in its datasource is empty?
I know that I can use a certain amount of JavaScript to manipulate data in the report. Is it possible to perhaps hide an element if a field value is NULL or empty?
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>
精彩评论