如何迭代列表并在每个groupFooter中逐个打印它的内容?

时间:2017-10-01 05:09:25

标签: list jasper-reports

我只是想知道,有没有办法迭代List并在每个groupFooter中逐个(增量)打印它的内容?

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

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

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

我认为您不应该尝试迭代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>
  

注意:printWhenExpression将避免IndexOutOfBoundsException,因此组号高于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>

输出,只要我们在List的大小范围内,就会看到列表中的值是如何提取的

output