如何从 JSON 源渲染 Jasper 报告中的嵌套表?

问题描述 投票:0回答:1

我有一个像这样的JSON

{
   "details": [
       {
           "name": "Name 1",
           "head": [
               {
                   "isin": "ISIN 1",
                   "coupon": "Coupon 1"
               }
           ]
       },
       {
           "name": "Name 2",
           "head": [
               {
                   "isin": "ISIN 2",
                   "coupon": "Coupon 2"
               }
           ]
       }
   ]
}

我想展示一个像这样的嵌套表格

姓名1
ISIN 1 优惠券1
姓名2
ISIN 2 优惠券2

我的报告在这里

<?xml version="1.0" encoding="UTF-8"?>
<jasperReport uuid="13bb06fa-a8ce-41f8-b3e4-a094f892f221">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="JSONAdapter"/>
    <subDataset name="datasetDetails" uuid="3f8c902f-fffd-48fe-b65f-46e976854490">
        <property name="com.jaspersoft.studio.data.defaultdataadapter" value="JSONAdapter"/>
        <queryString language="json">
            <![CDATA[details]]>
        </queryString>
        <field name="name" class="java.lang.String">
            <property name="net.sf.jasperreports.json.field.expression" value="name"/>
            <fieldDescription><![CDATA[name]]></fieldDescription>
        </field>
        <field name="head" class="net.sf.jasperreports.engine.data.JsonDataSource">
            <property name="net.sf.jasperreports.json.field.expression" value="head"/>
            <fieldDescription><![CDATA[head]]></fieldDescription>
        </field>
    </subDataset>
    <subDataset name="datasetDetailsHead" uuid="a91998cc-c1fa-4802-868d-90cb9a24636d">
        <property name="com.jaspersoft.studio.data.defaultdataadapter" value="JSONAdapter"/>
        <queryString language="json">
            <![CDATA[details.head]]>
        </queryString>
        <field name="isin" class="java.lang.String">
            <property name="net.sf.jasperreports.json.field.expression" value="isin"/>
            <fieldDescription><![CDATA[isin]]></fieldDescription>
        </field>
        <field name="coupon" class="java.lang.String">
            <property name="net.sf.jasperreports.json.field.expression" value="coupon"/>
            <fieldDescription><![CDATA[coupon]]></fieldDescription>
        </field>
    </subDataset>
    <queryString language="JSON">
        <![CDATA[]]>
    </queryString>
    <detail>
        <band>
            <componentElement>
                <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="datasetDetails" uuid="3ca38bbd-cd5a-44c4-98bb-2e993e3a4fc4">
                        <dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("details")]]></dataSourceExpression>
                    </datasetRun>
                    <jr:column>
                        <jr:detailCell>
                            <frame>
                                <textField>
                                    <textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>
                                </textField>
                            </frame>
                            <frame>
                                <componentElement>
                                    <jr:table>
                                        <datasetRun subDataset="datasetDetailsHeadBond" uuid="998a8505-69bb-493a-8c3a-5dc7a0e62d42">
                                            <dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("details.head")]]></dataSourceExpression>
                                        </datasetRun>
                                        <jr:column>
                                            <jr:detailCell>
                                                <textField>
                                                    <textFieldExpression><![CDATA[$F{isin}]]></textFieldExpression>
                                                </textField>
                                            </jr:detailCell>
                                        </jr:column>
                                        <jr:column >
                                            <jr:detailCell >
                                                <textField>
                                                    <textFieldExpression><![CDATA[$F{coupon}]]></textFieldExpression>
                                                </textField>
                                            </jr:detailCell>
                                        </jr:column>
                                    </jr:table>
                                </componentElement>
                            </frame>
                        </jr:detailCell>
                    </jr:column>
                </jr:table>
            </componentElement>
        </band>
    </detail>
</jasperReport>

但是,渲染报表时只能看到标题(名称 1、名称 2 ...),但看不到嵌套数据。

请问我该如何解决这个问题?

jasper-reports
1个回答
0
投票

嵌套表的数据集应使用相对于其父数据集的路径,即

subDataSource("head")
而不是
subDataSource("details.head")

                                    <datasetRun subDataset="datasetDetailsHeadBond" uuid="998a8505-69bb-493a-8c3a-5dc7a0e62d42">
                                        <dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("head")]]></dataSourceExpression>
                                    </datasetRun>
© www.soinside.com 2019 - 2024. All rights reserved.