Reputation: 55
JasperReports generate null to table cell if this value is not defined in table scope, but defined globally.
At the top of the report I have defined variables:
<subDataset name="Dataset1" uuid="c145c0b0-641e-4a32-8e07-265189715ef9">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="data\JSONDdapterNew.xml"/>
<queryString language="json">
<![CDATA[]]>
</queryString>
<field name="groupName" class="java.lang.String">
<fieldDescription><![CDATA[groupName]]></fieldDescription>
</field>
<field name="elementName" class="java.lang.String">
<fieldDescription><![CDATA[elementName]]></fieldDescription>
</field>
<field name="elementValue" class="java.lang.String">
<fieldDescription><![CDATA[elementValue]]></fieldDescription>
</field>
</subDataset>
Then I pass they to the Jasper table (markup simplified without dropping sense):
<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="Dataset1" uuid="db1f69ee-c0db-4ece-aacb-a181465bdc79">
<dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("elements")]]></dataSourceExpression>
</datasetRun>
<jr:column width="220" uuid="09e23518-5d7f-45d9-9a22-c4b537f0d83f">
<jr:detailCell style="Table 2_TD" height="18">
<textField isStretchWithOverflow="true">
<reportElement stretchType="RelativeToBandHeight" x="0" y="0" width="220" height="18" uuid="4d0b54e7-584e-4cc9-86f2-59d72b600f1b"/>
<textElement verticalAlignment="Middle">
<font size="9"/>
<paragraph leftIndent="5"/>
</textElement>
<textFieldExpression><![CDATA[$F{groupName}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
<jr:column width="220" uuid="09e23518-5d7f-45d9-9a22-c4b537f0d83f">
<jr:detailCell style="Table 2_TD" height="18">
<textField isStretchWithOverflow="true">
<reportElement stretchType="RelativeToBandHeight" x="0" y="0" width="220" height="18" uuid="4d0b54e7-584e-4cc9-86f2-59d72b600f1b"/>
<textElement verticalAlignment="Middle">
<font size="9"/>
<paragraph leftIndent="5"/>
</textElement>
<textFieldExpression><![CDATA[$F{elementName}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
<jr:column width="220" uuid="09e23518-5d7f-45d9-9a22-c4b537f0d83f">
<jr:detailCell style="Table 2_TD" height="18">
<property name="com.jaspersoft.studio.unit.width" value="px"/>
<textField isStretchWithOverflow="true">
<reportElement stretchType="RelativeToBandHeight" x="0" y="0" width="220" height="18" uuid="4d0b54e7-584e-4cc9-86f2-59d72b600f1b"/>
<textElement verticalAlignment="Middle">
<font size="9"/>
<paragraph leftIndent="5"/>
</textElement>
<textFieldExpression><![CDATA[$F{elementValue}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
</jr:table>
Example for input json:
[
{
"groupName" : "Group1",
"elements" : [
{
"elementName" : "el11",
"elementValue" : "evl1"
},
{
"elementName" : "el12",
"elementValue" : "evl2"
}
]
},
{
"groupName" : "Group2",
"elements" : [
{
"elementName" : "el21",
"elementValue" : "ev21"
},
{
"elementName" : "el22",
"elementValue" : "ev22"
}
]
}
]
As result I see values for elementName
and elementValue
, but for groupName
I see null
. I want to see something like that:
Group1 | el11 | ev11 |
Group1 | el12 | ev12 |
Group2 | el21 | ev21 |
Group2 | el22 | ev22 |
Upvotes: 0
Views: 1824
Reputation: 2521
If you really must use a table element and you have it in the Detail band, you could pass the groupName as a parameter to the table dataset:
First, you would add the groupName field to the main dataset:
<queryString language="json">
<![CDATA[]]>
</queryString>
<field name="groupName" class="java.lang.String">
<fieldDescription><![CDATA[groupName]]></fieldDescription>
</field>
The subDataset would become:
<subDataset name="Dataset1" uuid="c145c0b0-641e-4a32-8e07-265189715ef9">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="data\JSONDdapterNew.xml"/>
<queryString language="json">
<![CDATA[]]>
</queryString>
<parameter name="groupNameParam" class="java.lang.String"/>
<field name="elementName" class="java.lang.String">
<fieldDescription><![CDATA[elementName]]></fieldDescription>
</field>
<field name="elementValue" class="java.lang.String">
<fieldDescription><![CDATA[elementValue]]></fieldDescription>
</field>
</subDataset>
The table datasetRun would become:
<datasetRun subDataset="Dataset1" uuid="db1f69ee-c0db-4ece-aacb-a181465bdc79">
<datasetParameter name="groupNameParam">
<datasetParameterExpression><![CDATA[$F{groupName}]]></datasetParameterExpression>
</datasetParameter>
<dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("elements")]]></dataSourceExpression>
</datasetRun>
And then your textFieldExpression would go from $F{groupName}
to $P{groupNameParam}
.
The easier solution, that does not rely on a table element, would involve switching to the newer JSONQL(stable in Jaspersoft Studio since v6.4.0) language that allows traversals up the JSON tree:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.9.0.final using JasperReports Library version 6.9.0-cb8f9004be492ccc537180b49c026951f4220bf3 -->
<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="Report_v3" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="5c8f0a97-e66c-4103-8305-10e7cefe9ca2">
<queryString language="jsonql">
<![CDATA[elements.*]]>
</queryString>
<field name="groupName" class="java.lang.String">
<property name="net.sf.jasperreports.jsonql.field.expression" value="^^.groupName"/>
<fieldDescription><![CDATA[Group Name]]></fieldDescription>
</field>
<field name="elementName" class="java.lang.String">
<property name="net.sf.jasperreports.jsonql.field.expression" value="elementName"/>
<fieldDescription><![CDATA[Element Name]]></fieldDescription>
</field>
<field name="elementValue" class="java.lang.String">
<property name="net.sf.jasperreports.jsonql.field.expression" value="elementValue"/>
<fieldDescription><![CDATA[Element Value]]></fieldDescription>
</field>
<columnHeader>
<band height="30" splitType="Stretch">
<staticText>
<reportElement x="0" y="0" width="185" height="30" uuid="ef173e64-d7d6-4afd-abba-9ad32bf204b6">
<property name="com.jaspersoft.studio.spreadsheet.connectionID" value="b7c69e0c-11ed-4881-93c1-dd83cefb5d4c"/>
</reportElement>
<text><![CDATA[Group Name]]></text>
</staticText>
<staticText>
<reportElement x="185" y="0" width="185" height="30" uuid="cd0d90cb-2ced-4f52-becb-7e171c0e7824">
<property name="com.jaspersoft.studio.spreadsheet.connectionID" value="cef61be1-0d90-4575-8b39-00b300c52204"/>
</reportElement>
<text><![CDATA[Element Name]]></text>
</staticText>
<staticText>
<reportElement x="370" y="0" width="185" height="30" uuid="7bee32cf-b618-489d-bb81-014d410ba074">
<property name="com.jaspersoft.studio.spreadsheet.connectionID" value="9a5a388b-f4ea-48dd-9b4f-82a7c58b2c22"/>
</reportElement>
<text><![CDATA[Element Value]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="30" splitType="Stretch">
<textField>
<reportElement x="0" y="0" width="185" height="30" uuid="6866d5da-c4a4-4ae3-af9f-19d29e3d28dc">
<property name="com.jaspersoft.studio.spreadsheet.connectionID" value="b7c69e0c-11ed-4881-93c1-dd83cefb5d4c"/>
</reportElement>
<textFieldExpression><![CDATA[$F{groupName}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="185" y="0" width="185" height="30" uuid="87262f8a-1548-44a3-887e-e916aa551eab">
<property name="com.jaspersoft.studio.spreadsheet.connectionID" value="cef61be1-0d90-4575-8b39-00b300c52204"/>
</reportElement>
<textFieldExpression><![CDATA[$F{elementName}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="370" y="0" width="185" height="30" uuid="7bf9a789-1f10-4986-abd7-aa7c57bd1b9c">
<property name="com.jaspersoft.studio.spreadsheet.connectionID" value="9a5a388b-f4ea-48dd-9b4f-82a7c58b2c22"/>
</reportElement>
<textFieldExpression><![CDATA[$F{elementValue}]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
Upvotes: 3