Reputation: 67
I'm beginner to jasper and working on a report where I need to display the header of a table only when data is present in it. I have currently below code snippet in my jrxml:
<frame>
<reportElement stretchType="ContainerHeight" x="240" y="37" width="110" height="52" uuid="some-uuid">
<property name="com.jaspersoft.studio.unit.width" value="px"/>
</reportElement>
<staticText>
<reportElement positionType="Float" x="0" y="0" width="110" height="16" uuid="some-uuid">
<property name="com.jaspersoft.studio.unit.width" value="px"/>
</reportElement>
<textElement>
<font size="12" isBold="true"/>
</textElement>
<text><![CDATA[EMPLOYEES]]></text>
</staticText>
<componentElement>
<reportElement positionType="Float" x="0" y="16" width="110" height="16" isRemoveLineWhenBlank="true" uuid="some-uuid">
<property name="com.jaspersoft.studio.layout" value="com.jaspersoft.studio.editor.layout.VerticalRowLayout"/>
</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">
<datasetRun subDataset="employeesDataSet" uuid="some-uuid">
<dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("employees")]]></dataSourceExpression>
</datasetRun>
<jr:column width="110" uuid="some-uuid">
<property name="com.jaspersoft.studio.components.table.model.column.name" value="Column1"/>
<jr:detailCell height="16">
<textField isStretchWithOverflow="true">
<reportElement style="paragraph" stretchType="ElementGroupHeight" x="0" y="0" width="110" height="16" isRemoveLineWhenBlank="true" uuid="some-uuid"/>
<textElement markup="html">
<font size="12"/>
</textElement>
<textFieldExpression><![CDATA["<B>" +($F{type} != null ? $F{type} : "") + "</b> " + ($F{result} != null ? $F{result} : "") + ", " + ($F{value} != null ? $F{value} : "") + " " + ($F{unit} != null ? $F{unit} : "")]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
</jr:table>
</componentElement>
</frame>
JSON from which data is being written to report looks like:
"employees": [
{
"type": "HR",
"result": "1",
"value": "Positive"
},
{
"type": "MD",
"result": "3",
"value": "Positive"
}
]
How can I display the static header EMPLOYEES
only when data is present and not display anything when there is no data in JSON?
I have tried adding in <static text>
but it didn't work :
<printWhenExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("employees") != null]]></printWhenExpression>
Upvotes: 0
Views: 1058
Reputation: 67
I have found the solution and posting it here so that other people facing similar issue can refer.
To check if json array has more elements, use next() method. This line worked for me :
<printWhenExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("employees").next()]]></printWhenExpression>
Upvotes: 1