Search code examples
jasper-reports

How to use the same REPORT_COUNT in multiple tables incrementing it?


I'm using iReport 5.6 to generate reports for my JavaApp, I have three datasets where as bellow :

dataset1 query

SELECT * FROM players s WHERE s.role = 1

dataset2 query

SELECT * FROM players s WHERE s.role = 2

dataset3 query

SELECT * FROM players s WHERE s.role = 3

then I have created 3 Detail bands where I have put :

  • Detail 1 contains table1 that uses dataset1 : in this table I defined the headers that are the same for the other tables (2 and 3)

  • Detail 2 contains table2 that uses dataset2

  • Detail 3 contains table3 that uses dataset3

What I can't do is to add a count column using the $V{REPORT_COUNT} jasper variable for the three table that doesn't refresh the counting as seen in the picture below I want this counting to continue the incrementation.


Solution

  • To achieve this you can use the possibility to specify returnValue from datasetRun (table) and then pass this to next datasetRun (table) as a datasetParameter

    In example we will pass as returnValue the $V{REPORT_COUNT} (of the table) to a variabile in main report $V{currentRecordCnt}, the currentRecordCnt we will then pass to next datasetRun (table 2) and use this to increment a variabile in 2 dataset

    <variable name="accRecordCnt" class="java.lang.Integer">
        <variableExpression><![CDATA[$P{currentRecordCnt}+$V{REPORT_COUNT}]]></variableExpression>
    </variable>
    

    We then pass the accRecordCnt back to main report's currentRecordCnt and repeat same for table 3.

    Full jrxml example

    <?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="AccuSum" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="85a44d77-bc70-4059-a88a-60aca0ef6bf0">
        <property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
        <subDataset name="Dataset1" uuid="5a00c263-2028-4446-948e-d614136ec5d7">
            <queryString>
                <![CDATA[]]>
            </queryString>
        </subDataset>
        <subDataset name="Dataset2" uuid="3ff8e779-2e60-403c-81f8-1d20fd04fc78">
            <parameter name="currentRecordCnt" class="java.lang.Integer"/>
            <queryString>
                <![CDATA[]]>
            </queryString>
            <variable name="accRecordCnt" class="java.lang.Integer">
                <variableExpression><![CDATA[$P{currentRecordCnt}+$V{REPORT_COUNT}]]></variableExpression>
            </variable>
        </subDataset>
        <subDataset name="Dataset3" uuid="3ff8e779-2e60-403c-81f8-1d20fd04fc78">
            <parameter name="currentRecordCnt" class="java.lang.Integer"/>
            <queryString>
                <![CDATA[]]>
            </queryString>
            <variable name="accRecordCnt" class="java.lang.Integer">
                <variableExpression><![CDATA[$P{currentRecordCnt}+$V{REPORT_COUNT}]]></variableExpression>
            </variable>
        </subDataset>
        <queryString>
            <![CDATA[]]>
        </queryString>
        <variable name="currentRecordCnt" class="java.lang.Integer"/>
        <detail>
            <band height="54" splitType="Stretch">
                <componentElement>
                    <reportElement x="0" y="10" width="540" height="30" uuid="c46d16ff-7b32-4481-8d89-0435f34f7b32">
                        <property name="com.jaspersoft.studio.layout" value="com.jaspersoft.studio.editor.layout.VerticalRowLayout"/>
                    </reportElement>
                    <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" uuid="6b893feb-3e07-4393-ae4d-30b64e3dbaf5">
                            <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.JREmptyDataSource(5)]]></dataSourceExpression>
                            <returnValue fromVariable="REPORT_COUNT" toVariable="currentRecordCnt"/>
                        </datasetRun>
                        <jr:column width="160" uuid="3b6f8589-0c6d-4709-a8cd-6cf642fd4ec9">
                            <jr:detailCell height="30">
                                <textField>
                                    <reportElement x="0" y="0" width="160" height="30" uuid="a04fcd2b-e179-4fdb-8eac-31da14721c9a"/>
                                    <textFieldExpression><![CDATA[$V{REPORT_COUNT}]]></textFieldExpression>
                                </textField>
                            </jr:detailCell>
                        </jr:column>
                    </jr:table>
                </componentElement>
            </band>
            <band height="50">
                <componentElement>
                    <reportElement x="0" y="10" width="540" height="30" uuid="6866e777-15b8-49a7-b00f-3ce3593c16c0">
                        <property name="com.jaspersoft.studio.layout" value="com.jaspersoft.studio.editor.layout.VerticalRowLayout"/>
                    </reportElement>
                    <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="Dataset2" uuid="f71b2649-27c0-4a30-bb68-8bc7c8a26100">
                            <datasetParameter name="currentRecordCnt">
                                <datasetParameterExpression><![CDATA[$V{currentRecordCnt}]]></datasetParameterExpression>
                            </datasetParameter>
                            <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.JREmptyDataSource(5)]]></dataSourceExpression>
                            <returnValue fromVariable="accRecordCnt" toVariable="currentRecordCnt"/>
                        </datasetRun>
                        <jr:column width="160" uuid="6e116b9f-cf9f-4a27-9e9a-892263482caf">
                            <property name="com.jaspersoft.studio.components.table.model.column.name" value="Column1"/>
                            <jr:detailCell height="30">
                                <textField>
                                    <reportElement x="0" y="0" width="160" height="30" uuid="8cb085f1-5155-4b17-9304-c3bb616ac965"/>
                                    <textFieldExpression><![CDATA[$V{accRecordCnt}]]></textFieldExpression>
                                </textField>
                            </jr:detailCell>
                        </jr:column>
                    </jr:table>
                </componentElement>
            </band>
            <band height="50">
                <componentElement>
                    <reportElement x="0" y="10" width="540" height="30" uuid="338527a7-5c7e-419d-ac85-658ac2d30655">
                        <property name="com.jaspersoft.studio.layout" value="com.jaspersoft.studio.editor.layout.VerticalRowLayout"/>
                    </reportElement>
                    <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="Dataset3" uuid="8c00ef2e-d328-42a8-8ba5-12bfde8225c0">
                            <datasetParameter name="currentRecordCnt">
                                <datasetParameterExpression><![CDATA[$V{currentRecordCnt}]]></datasetParameterExpression>
                            </datasetParameter>
                            <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.JREmptyDataSource(5)]]></dataSourceExpression>
                        </datasetRun>
                        <jr:column width="160" uuid="7e28ae8d-b64b-42fa-a5eb-4213198d1341">
                            <property name="com.jaspersoft.studio.components.table.model.column.name" value="Column1"/>
                            <jr:detailCell height="30">
                                <textField>
                                    <reportElement x="0" y="0" width="160" height="30" uuid="05900214-f740-4742-bae3-f4a97aed073e"/>
                                    <textFieldExpression><![CDATA[$V{accRecordCnt}]]></textFieldExpression>
                                </textField>
                            </jr:detailCell>
                        </jr:column>
                    </jr:table>
                </componentElement>
            </band>
        </detail>
    </jasperReport>
    

    Output (with 1 empty record)

    result