为什么我无法在 jrtable 中汇总 javabean 子项的值?

Why am I unable to sum up javabean subitem's value in jrtable?

我在对嵌套列值求和时遇到此异常

net.sf.jasperreports.engine.design.JRValidationException: Report design not valid : 
 1. Field not found : episodeCount
 2. Variable not found : episodeTotal

如何将上述 parameter/variable(s) 传递给子数据集?

我的jrxml文件

<subDataset name="dataset1">
    <field name="orderItem" class="com.blahblah.OrderItemPDF">
        <fieldDescription><![CDATA[_THIS]]></fieldDescription>
    </field>
</subDataset>

<field name="orderItems" class="java.util.Collection"/>
<field name="episodeCount" class="java.lang.Integer">
    <fieldDescription><![CDATA[$F{orderItem}.getEpisodeCount()]]></fieldDescription>
</field>

<variable name="episodeTotal" class="java.lang.Integer" calculation="Sum">
    <variableExpression><![CDATA[$F{episodeCount}]]></variableExpression>
</variable>

<title>
    <band height="113" splitType="Stretch">

    </band>
</title>
<pageHeader>
    <band height="360">
        <componentElement>
            <reportElement x="67" y="181" width="360" height="100"/>
            <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">
                    <dataSourceExpression>
                        <![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{orderItems})]]></dataSourceExpression>
                </datasetRun>
                <jr:column width="130">
                    <jr:tableHeader height="30">
                        <staticText>
                            <reportElement x="0" y="0" width="130" height="20"/>
                            <textElement/>
                            <text><![CDATA[Media Families]]></text>
                        </staticText>
                    </jr:tableHeader>
                    <jr:detailCell height="20">
                        <textField>
                            <reportElement x="0" y="0" width="130" height="20"/>
                            <textElement/>
                            <textFieldExpression>
                                <![CDATA[$F{orderItem}.getMediaFamilyName()]]></textFieldExpression>
                        </textField>
                    </jr:detailCell>
                </jr:column>
                <jr:column width="130">
                    <jr:tableHeader height="30">
                        <staticText>
                        <reportElement x="0" y="0" width="130" height="20"/>
                        <textElement/>
                        <text><![CDATA[Episodes]]></text>
                        </staticText>
                    </jr:tableHeader>
                    <jr:detailCell height="20">
                        <textField>
                            <reportElement x="0" y="0" width="130" height="20"/>
                            <textElement/>
                            <textFieldExpression><![CDATA[$F{orderItem}.getEpisodeCount()]]></textFieldExpression>
                        </textField>
                    </jr:detailCell>
                </jr:column>
                <jr:column width="60">
                    <jr:tableHeader height="30">
                        <staticText>
                            <reportElement x="0" y="0" width="60" height="20"/>
                            <textElement/>
                            <text><![CDATA[AdUnits]]></text>
                        </staticText>
                    </jr:tableHeader>
                    <!--<jr:columnFooter height="20">-->
                        <!--<textField>-->
                            <!--<reportElement x="0" y="0" width="130" height="20"/>-->
                            <!--<textElement/>-->
                            <!--<textFieldExpression><![CDATA[$V{episodeTotal}]]></textFieldExpression>-->
                        <!--</textField>-->
                    <!--</jr:columnFooter>-->
                    <jr:detailCell height="20">
                        <textField>
                            <reportElement x="0" y="0" width="60" height="20"/>
                            <textElement/>
                            <textFieldExpression><![CDATA[$F{orderItem}.getAdUnitCount()]]></textFieldExpression>
                        </textField>
                    </jr:detailCell>
                </jr:column>
            </jr:table>
        </componentElement>
    </band>
</pageHeader>
<columnHeader>jas
</columnHeader>
<detail>
    <band/>
</detail>

在 jrxml 外部定义的变量和参数以及资源无法从数据集或直接 Table 访问。您需要将这些作为参数显式传递给数据集。否则,它会出现 Not found 错误。

类似于 Passing main parameter to sub-datasets in JasperStudio

使用 subDataset 时,您需要在 subDataset 的 中定义所有字段、参数和变量 。您可以引用外部字段、参数和变量。

你的例子中变量的定义里面subDataset会是这样的

<subDataset name="dataset1">
    <field name="orderItem" class="com.blahblah.OrderItemPDF">
        <fieldDescription><![CDATA[_THIS]]></fieldDescription>
    </field>
    <variable name="episodeTotal" class="java.lang.Integer" calculation="Sum">
        <variableExpression><![CDATA[$F{orderItem}.getEpisodeCount()]]></variableExpression>
    </variable>
</subDataset>