如何迭代一个List并在每个groupFooter中一个一个打印它的内容?

How to iterate a List and print it's content one by one in each groupFooter?

我只想知道,有没有什么方法可以迭代一个List,并在每个groupFooter中一个一个(增量)打印它的内容?

我在我的报告中创建了一个组,在每个 groupFooter 部分,我想显示来自 java.util.List 我通过参数从 Java class 发送的内容。

目前我只是在我的 groupFooter 中使用 jr:listjr:listContents,结果是 列表中的所有内容 打印在 每个groupFooter。我很头疼要解决这个问题,所以任何帮助都会让我放心。

您需要创建一个在您的组级别递增的变量。 第 2 步在简单的文本字段或您想要的组件(不是集合组件)中使用该变量,并像这样放置他的表达式:list.indexOf(countVariable)

我认为你不应该尝试迭代List来获取一定数量的内容groupFooter,我会直接根据索引获取内容。

我们需要的是一个简单的指针(列表中的位置),在您的情况下,每次我们有一个新组时,这个数字似乎都是一个递增的数字。

算作组变化的变量是:

<variable name="countMyGroup" class="java.lang.Integer" incrementType="Group" incrementGroup="Group1" calculation="Count">
    <variableExpression><![CDATA[""]]></variableExpression>
    <initialValueExpression><![CDATA[0]]></initialValueExpression>
</variable> 

有了这个简单的变量,我们现在将获得显示的当前组数,我们可以添加一个 textField 和来自的值或 List 使用这个数字。见 java.util.List.get(int index)

<parameter name="listOfStuff" class="java.util.List" isForPrompting="false"/>
.....
<textField>
    <reportElement x="120" y="0" width="267" height="17" uuid="b45699d3-5d34-4d88-b7bc-2666cf787ace">
    <printWhenExpression><![CDATA[$P{listOfStuff}.size()>=$V{countMyGroup}]]></printWhenExpression>
    </reportElement>
    <textFieldExpression><![CDATA[$P{listOfStuff}.get($V{countMyGroup}-1)]]></textFieldExpression>
</textField>

Note: the printWhenExpression will avoid IndexOutOfBoundsException, hence that the group number is higher then the size of our List

完整的 jrxml 示例,它在每条记录上使用一个虚拟组更改,尝试将 JREmptyDatasource 与几条记录一起使用。

<?xml version="1.0" encoding="UTF-8"?>
<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="ListOnEachPage" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="33394f25-66fc-431b-ac82-88660e9115e5">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="Empty 4 records"/>
    <parameter name="listOfStuff" class="java.util.List" isForPrompting="false">
        <defaultValueExpression><![CDATA[Arrays.asList(new String[]{"group 1","group 2","group 3"})]]></defaultValueExpression>
    </parameter>
    <queryString>
        <![CDATA[]]>
    </queryString>
    <variable name="countMyGroup" class="java.lang.Integer" incrementType="Group" incrementGroup="Group1" calculation="Count">
        <variableExpression><![CDATA[""]]></variableExpression>
        <initialValueExpression><![CDATA[0]]></initialValueExpression>
    </variable>
    <group name="Group1">
        <groupExpression><![CDATA[$V{REPORT_COUNT}]]></groupExpression>
        <groupFooter>
            <band height="34">
                <textField>
                    <reportElement x="120" y="0" width="267" height="17" uuid="b45699d3-5d34-4d88-b7bc-2666cf787ace">
                        <printWhenExpression><![CDATA[$P{listOfStuff}.size()>=$V{countMyGroup}]]></printWhenExpression>
                    </reportElement>
                    <textFieldExpression><![CDATA[$P{listOfStuff}.get($V{countMyGroup}-1)]]></textFieldExpression>
                </textField>
                <textField>
                    <reportElement x="445" y="0" width="100" height="17" uuid="e5c46332-b137-4c55-99e4-265c87b8f97d"/>
                    <textFieldExpression><![CDATA[$V{countMyGroup}]]></textFieldExpression>
                </textField>
            </band>
        </groupFooter>
    </group>
    <detail>
        <band height="63" splitType="Stretch">
            <staticText>
                <reportElement x="140" y="20" width="264" height="30" uuid="1ec9f950-dd2d-4c18-a81a-b0da937eb1b5"/>
                <textElement>
                    <font size="14"/>
                </textElement>
                <text><![CDATA[Just some text to simulate detail band]]></text>
            </staticText>
        </band>
    </detail>
</jasperReport>

Output,只要我们在 List

的大小范围内,看看如何从列表中提取值