1

In my JasperReport project using Jaspersoft Studio, I would like to create a column in my table that is the sum of the current page number (where the table is) and the current row number (starting from 1) like this :

+------------------------+-------------+---------------+
| Summary | Current page | Current row | Page of Title |
+------------------------+-------------+---------------+
| Title 1 |            4 |           1 |             5 |
+------------------------+-------------+---------------+
| Title 2 |            4 |           2 |             6 |
+------------------------+-------------+---------------+
| Title 3 |            4 |           3 |             7 |
+------------------------+-------------+---------------+

My table is in a Subreport so I tried to sum up ${MASTER_CURRENT_PAGE} and ${REPORT_COUNT} but the first must be evaluated to Master and the second to Now to have correct values.

I am learning to use the software on the job, I saw that I could create custom variables - it may be the solution - but I don't know how to do what I want.

Does a ninja have the solution?

Petter Friberg
  • 21,252
  • 9
  • 60
  • 109

1 Answers1

0

My idea is this, pass the master reports $V{PAGE_NUMBER} to sub report

<subreportParameter name="MASTER_PAGE_NR">
    <subreportParameterExpression><![CDATA[$V{PAGE_NUMBER}]]></subreportParameterExpression>
</subreportParameter>

and then use the parameter to sum in sub report (evaluation time now), but watch out!, your subreport may overflow to new page hence you need to take this into consideration, which you can do by using the current page number in the subreport. The actual page number will be $P{MASTER_PAGE_NUMBER} + $V{PAGE_NUMBER} - 1, in your textField "Page of Title" it will be:

<textFieldExpression>
    <![CDATA[$V{REPORT_COUNT} +$P{MASTER_PAGE_NUMBER} + $V{PAGE_NUMBER} - 1]]>
</textFieldExpression>

Full example

Master jrxml

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.2.1.final using JasperReports Library version 5.1.2  -->
<!-- 2018-09-27T16:21:23 -->
<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_9" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="f44e296b-0657-45aa-9089-0bcf771dfac4">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="Empty 3"/>
    <queryString>
        <![CDATA[]]>
    </queryString>
    <detail>
        <band height="51" splitType="Stretch">
            <subreport>
                <reportElement x="0" y="0" width="550" height="50" uuid="1364b663-c783-4686-b571-7a579b3b5052"/>
                <subreportParameter name="MASTER_PAGE_NUMBER">
                    <subreportParameterExpression><![CDATA[$V{PAGE_NUMBER}]]></subreportParameterExpression>
                </subreportParameter>
                <dataSourceExpression><![CDATA[new JREmptyDataSource(40)]]></dataSourceExpression>
                <subreportExpression><![CDATA["Blank_Subreport.jasper"]]></subreportExpression>
            </subreport>
        </band>
    </detail>
</jasperReport>

Sub report 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_Subreport" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="351ed03b-edc6-47a9-9139-f7be09f27889">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
    <parameter name="MASTER_PAGE_NUMBER" class="java.lang.Integer" isForPrompting="false">
        <defaultValueExpression><![CDATA[1]]></defaultValueExpression>
    </parameter>
    <queryString>
        <![CDATA[]]>
    </queryString>
    <columnHeader>
        <band height="20" splitType="Stretch">
            <staticText>
                <reportElement x="0" y="0" width="100" height="20" uuid="d55531d4-77ea-4022-8409-a49dc8d80719"/>
                <textElement textAlignment="Center" verticalAlignment="Middle"/>
                <text><![CDATA[Summary]]></text>
            </staticText>
            <staticText>
                <reportElement x="100" y="0" width="100" height="20" uuid="d329710d-6ba8-404c-ae63-d9c7388a7a9c"/>
                <textElement textAlignment="Center" verticalAlignment="Middle"/>
                <text><![CDATA[Current Page]]></text>
            </staticText>
            <staticText>
                <reportElement x="200" y="0" width="100" height="20" uuid="46562462-4658-489a-9028-ac5d041bae1d"/>
                <textElement textAlignment="Center" verticalAlignment="Middle"/>
                <text><![CDATA[Current Row]]></text>
            </staticText>
            <staticText>
                <reportElement x="300" y="0" width="100" height="20" uuid="2ea0046b-aece-4fb3-96f0-20f94a4f2510"/>
                <textElement textAlignment="Center" verticalAlignment="Middle"/>
                <text><![CDATA[Page of Title]]></text>
            </staticText>
        </band>
    </columnHeader>
    <detail>
        <band height="23" splitType="Stretch">
            <property name="com.jaspersoft.studio.unit.height" value="pixel"/>
            <textField>
                <reportElement x="0" y="0" width="100" height="20" uuid="c168f2fa-f0a2-4db1-af3a-c82933395133"/>
                <textElement textAlignment="Center" verticalAlignment="Middle"/>
                <textFieldExpression><![CDATA["Title " + $V{REPORT_COUNT}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="100" y="0" width="100" height="20" uuid="bbc49b4a-c3f2-4167-be35-c37bc813f89c"/>
                <textElement textAlignment="Center" verticalAlignment="Middle"/>
                <textFieldExpression><![CDATA[$P{MASTER_PAGE_NUMBER}+$V{PAGE_NUMBER}-1]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="200" y="0" width="100" height="20" uuid="85c1b417-7224-4dd9-9917-4585b579c9d7"/>
                <textElement textAlignment="Center" verticalAlignment="Middle"/>
                <textFieldExpression><![CDATA[$V{REPORT_COUNT}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="300" y="0" width="100" height="20" uuid="6b0fdd30-a6be-47d3-8819-146d0fb67249"/>
                <textElement textAlignment="Center" verticalAlignment="Middle"/>
                <textFieldExpression><![CDATA[$V{REPORT_COUNT} +$P{MASTER_PAGE_NUMBER}+$V{PAGE_NUMBER}-1]]></textFieldExpression>
            </textField>
        </band>
    </detail>
</jasperReport>

Output of page 2 table on page 1 is overflowing on page 2 (running 3 empty records on main, passing 40 empty records to subreport)

result

Petter Friberg
  • 21,252
  • 9
  • 60
  • 109
  • Unfortunately it does not work for me : the variable MASTER_PAGE_NUMBER remains stuck at 1. I'm on page 4, and according to the XML file I send, it could become page 3. Maybe because I forget to mention that my table is related to a subDataset. I tried to pass MASTER_PAGE_NUMBER from subreport to subDataset without success. – Sébastien Colbe Sep 27 '18 at 15:51
  • @SébastienColbe Pass the sum of MASTER_PAGE number and PAGE_NUMBER to the subDataset, hence if you have a subreport, it will have the page it start from master, then in subreport you have PAGE_NUMBER, so if you have another subreport or subdataset you just need to continue the counting passing it as parameter down. Example pass $P{MASTER_PAGE_NUMBER} + $V{PAGE_NUMBER} to subdatset in a parameter that is called MASTER_PAGE_NUMBER of the subdataset and continue with example above. – Petter Friberg Sep 27 '18 at 15:59
  • Still not working. Maybe because of my structure : my report contains one subreport that contains all my pages. Do I have to split my subreport into several subreports into my report? – Sébastien Colbe Sep 27 '18 at 16:19
  • And what does the subreport contain, other subreport?, table component? If table component in which band? – Petter Friberg Sep 27 '18 at 16:58
  • Sorry, I will describe my single subreport in my report : It contains a Footer and 3 Detail sections that contains sureports, tables and charts. I want to do this in the last Detail section. • The format is A4 (width: 595px, height: 842px). • My single subreport in report have paddings (20px everywhere). • I fill my report with an external XML file as parameter (Xpath). • All my tables have their subdataset and are positionType="FixRelativeToTop" and stretchType="NoStretch". – Sébastien Colbe Sep 28 '18 at 07:53
  • • My first Detail section is maximized (772px) and contains the header, a title, a subreport with a pie chart and two tables. • My second Detail section is stretch to content (180 px) and contains a frame that contains a title, a table and some frame after containing text fields. In a field of this table, I used "Stretch with overflow". • My third Detail section is stretch to content (90px) and contains a frame that contains a title, the table I described before and some frame after containing text fields. • My Footer (30px) contains only a text field. Tell me if you need further info :3 – Sébastien Colbe Sep 28 '18 at 07:53
  • The table is in another subreport or are you using the jr:table component? – Petter Friberg Sep 28 '18 at 12:01
  • The table is a jr:table component. Sorry for this late response, I couldn't be on my computer this weekend and until now I had to work on another project. – Sébastien Colbe Oct 02 '18 at 15:59