Davi Bogo
Davi Bogo

Reputation: 149

How to repeat textfield by parameter in jaspersoft?

I have a text for a document, that can repeat itself for more than 100 times, what I was trying to do is putting a lot of textfield's and then putting a print when expression so that only the quantity of the parameters that I gave would repeat.

The problem is that it can repeat more times than I thought. I like to pass as parameter the number of times it should repeat, not using datasource.

Upvotes: 0

Views: 1659

Answers (1)

Petter Friberg
Petter Friberg

Reputation: 21710

An easy way to repeat text would be to actually use a datasource, the JREmptyDataSource in constructor you can define how many records you like.

I will show an example using the jr:list component to repeat text, but you could use the jr:table component or a subreport instead.

Pass to this component an empty datasource with the number of records you like (hence the parameter defined):

<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.JREmptyDataSource($P{NR_REPEAT})]]></dataSourceExpression>

Full jrxml

<?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="Blank_A4" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="716e18ba-d975-40fa-9be2-89f43a4ab69c">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
    <subDataset name="RepeatDataset" uuid="185d5db2-6f6b-4c9a-9905-e7a554b6a8fa">
        <queryString>
            <![CDATA[]]>
        </queryString>
    </subDataset>
    <parameter name="NR_REPEAT" class="java.lang.Integer">
        <defaultValueExpression><![CDATA[5]]></defaultValueExpression>
    </parameter>
    <queryString>
        <![CDATA[]]>
    </queryString>
    <title>
        <band height="22">
            <textField>
                <reportElement x="0" y="0" width="260" height="20" uuid="b0a2bf35-f015-4b85-aa87-b5587e48de10"/>
                <textElement verticalAlignment="Middle"/>
                <textFieldExpression><![CDATA["NR. REPEAT IS: " + $P{NR_REPEAT}]]></textFieldExpression>
            </textField>
        </band>
    </title>
    <detail>
        <band height="31" splitType="Stretch">
            <componentElement>
                <reportElement x="0" y="0" width="550" height="22" uuid="0d5f8ed2-c697-4337-b0b0-2411da8cc5fa">
                    <property name="com.jaspersoft.studio.unit.height" value="px"/>
                </reportElement>
                <jr:list xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd" printOrder="Vertical">
                    <datasetRun subDataset="RepeatDataset" uuid="e98a25f7-2d4e-403e-8199-0d3573bb3a3e">
                        <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.JREmptyDataSource($P{NR_REPEAT})]]></dataSourceExpression>
                    </datasetRun>
                    <jr:listContents height="22" width="550">
                        <textField>
                            <reportElement x="0" y="0" width="550" height="22" uuid="21984a90-5517-4c8b-82f0-1fe682728830">
                                <property name="com.jaspersoft.studio.unit.height" value="px"/>
                            </reportElement>
                            <textElement verticalAlignment="Middle"/>
                            <textFieldExpression><![CDATA[$V{REPORT_COUNT} + " - Hello world"]]></textFieldExpression>
                        </textField>
                    </jr:listContents>
                </jr:list>
            </componentElement>
        </band>
    </detail>
</jasperReport>

Output setting NR_REPEAT to 5

result

Upvotes: 1

Related Questions