贾斯珀报道。在子 table 中使用父 json 的值

JasperReports. Using value from parent json in child table

如果此值未在 table 范围内定义,而是在全局范围内定义,则 JasperReports 会向 table 单元格生成空值。

在报告的顶部我定义了变量:

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

然后我将它们传递给 Jasper table(简化标记但不失意义):

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

输入示例 json:

[
    {
        "groupName" : "Group1",
        "elements" : [
            {
                "elementName" : "el11",
                "elementValue" : "evl1"
            },
            {
                "elementName" : "el12",
                "elementValue" : "evl2"
            }
        ]
    },
    {
        "groupName" : "Group2",
        "elements" : [
            {
                "elementName" : "el21",
                "elementValue" : "ev21"
            },
            {
                "elementName" : "el22",
                "elementValue" : "ev22"
            }
        ]
    }   
]

因此,我看到了 elementNameelementValue 的值,但对于 groupName,我看到了 null。我想看到这样的东西:

第 1 组 | el11 | ev11 |

第 1 组 | EL12 | ev12 |

第 2 组 | EL21 | ev21 |

第 2 组 | EL22 | ev22 |

如果您确实必须使用 table 元素并且您在详细信息带中有它,您可以将 groupName 作为参数传递给 table 数据集:

首先,您要将 groupName 字段添加到主数据集:

<queryString language="json">
    <![CDATA[]]>
</queryString>
<field name="groupName" class="java.lang.String">
    <fieldDescription><![CDATA[groupName]]></fieldDescription>
</field>

子数据集将变为:

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

table datasetRun 将变为:

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

然后您的 textFieldExpression 将从 $F{groupName} 变为 $P{groupNameParam}

不依赖于 table 元素的更简单的解决方案将涉及切换到较新的 JSONQL(stable in Jaspersoft Studio since v6.4.0)语言允许向上遍历 JSON 树:

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