在iReport 5.6.0中:当我使用JRBeanCollectionDataSource时,为什么我的表元素中缺少第一条记录?

时间:2017-10-25 12:28:51

标签: java jasper-reports

从我的应用程序调用jasper报告使用以下代码:

List<TransactionResponse> listobject = new ArrayList<TransactionResponse>();
................
JRBeanCollectionDataSource dataSource = new JRBeanCollectionDataSource(listobject);
Map<String, Object> parameters = new HashMap<String,Object>();
parameters.put("transactionResponse", dataSource );
.........
jasperPrint  = JasperFillManager.fillReport(reportTemplate, parameters, dataSource);

和JRXML报告: follow same step

但仍然是第一张不打印的记录。

JRXML:1)在报告中声明一个新的数据集  参数名称“inquiryResponse”与JRBeanCollectionDataSource数据类型

<subDataset name="transactionResponseDataset" uuid="1a6f1035-fd54-4e45-8d74-e8ee2693d878">
    <queryString language="SQL">
        <![CDATA[]]>
    </queryString>
    <field name="amount" class="java.lang.String">
        <fieldDescription><![CDATA[billNetAmount]]></fieldDescription>
    </field>
    <field name="CustomerId" class="java.lang.String">
        <fieldDescription><![CDATA[customerId]]></fieldDescription>
    </field>
</subDataset>
<parameter name="inquiryResponse" class="net.sf.jasperreports.engine.data.JRBeanCollectionDataSource"/>

2)详情表中的表组件:

<componentElement>
            <reportElement key="table 3" stretchType="RelativeToTallestObject" x="0" y="0" width="802" height="75" isPrintWhenDetailOverflows="true" forecolor="#FFFFFF" uuid="7ee968c9-0c6a-4f3e-a8f6-03da6a599429"/>
            <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="transactionResponseDataset" uuid="e577da21-baf8-49e6-89ae-b72ef1240a4c">
                    <dataSourceExpression><![CDATA[$P{inquiryResponse}]]></dataSourceExpression>
                </datasetRun>
                <jr:column width="100" uuid="f7289bee-9305-408f-8a06-8bf479643735">
                    <jr:columnHeader style="table" height="71" rowSpan="1">
                        <staticText>
                            <reportElement x="0" y="0" width="100" height="71" uuid="253bf2ab-ffb6-40cc-a7f8-11f84fe9001a"/>
                            <box topPadding="5" leftPadding="3" bottomPadding="2" rightPadding="3"/>
                            <textElement textAlignment="Center" verticalAlignment="Top">
                                <font fontName="SansSerif" size="11" isBold="true"/>
                            </textElement>
                            <text><![CDATA[AMOUNT]]></text>
                        </staticText>
                    </jr:columnHeader>
                    <jr:detailCell style="table 1_TD" height="63" rowSpan="1">
                        <textField isStretchWithOverflow="true" isBlankWhenNull="true">
                            <reportElement stretchType="RelativeToTallestObject" x="0" y="0" width="100" height="63" uuid="e03b99f0-8973-438e-8eb8-f030b031444a"/>
                            <box topPadding="2" leftPadding="2" bottomPadding="2" rightPadding="2"/>
                            <textElement>
                                <font fontName="SansSerif"/>
                            </textElement>
                            <textFieldExpression><![CDATA[$F{billNetAmount}]]></textFieldExpression>
                        </textField>
                    </jr:detailCell>
                </jr:column>
                <jr:column width="94" uuid="ce1d342c-5f74-4329-91a9-8b2d10c3dfbc">
                    <jr:tableFooter height="30" rowSpan="1"/>
                    <jr:columnHeader style="table" height="71" rowSpan="1">
                        <staticText>
                            <reportElement x="0" y="0" width="94" height="71" uuid="25eae146-8aa4-46d5-add1-ae60c154eb3a"/>
                            <box topPadding="5" leftPadding="3" bottomPadding="2" rightPadding="3"/>
                            <textElement textAlignment="Center" verticalAlignment="Top">
                                <font fontName="SansSerif" size="11" isBold="true"/>
                            </textElement>
                            <text><![CDATA[CustomerId]]></text>
                        </staticText>
                    </jr:columnHeader>
                    <jr:detailCell style="table 1_TD" height="63" rowSpan="1">
                        <textField isStretchWithOverflow="true" isBlankWhenNull="true">
                            <reportElement stretchType="RelativeToTallestObject" x="0" y="0" width="94" height="63" uuid="fdd054df-7c76-4a1d-8faf-a93e9737c837"/>
                            <box topPadding="2" leftPadding="2" bottomPadding="2" rightPadding="2"/>
                            <textElement>
                                <font fontName="SansSerif"/>
                            </textElement>
                            <textFieldExpression><![CDATA[$F{customerId}]]></textFieldExpression>
                        </textField>
                    </jr:detailCell>
                </jr:column>
            </jr:table>
        </componentElement>

1 个答案:

答案 0 :(得分:1)

当人们在报告中两次使用相同的dataSource时,通常会发生这种情况:

  • 一次为主数据集,填写报告时,
  • 再次为基于子数据集的组件或放置在详细信息带中的子报告

当表组件重用相同的dataSource时,您的主数据集已经消耗了第一条记录。

在您的情况下,您可以通过以下方式轻松解决此问题:

  • 填写时使用所谓的空dataSource:

    jasperPrint = JasperFillManager.fillReport(reportTemplate, parameters, new JREmptyDataSource());
    
  • 或在将其作为参数传递时使用克隆:

    parameters.put("transactionResponse", dataSource.cloneDataSource());