Search code examples
jasper-reportsincrementatomic

How to create a counter for non empty fields in Jaspersoft?


I have some textfield's uses String variables that increments one Atomic Integer with "getAndIncrement()", I use this for my footnote, after the text of the String variable I put some mapping in my Report_Parameters_Map, for future usages.

The problem is that sometimes the textfield's are blank, but even when they aren't showing up, my Atomic Integer is incrementing, because the variable is executing the increment. so I have something like this:

text 1 text.
text 2 text.
(third is blank)
text 4 text.

This is wrong, as when I'm going to reference the footnote, it is showing notes from footnotes that doesn't even is there, just like that:

WRONG                                         RIGHT
text 1.                                       text 1.
(second is blank)                             (second is blank)
text 3.                                       text 2.

note 1...                                     note 1...
note 2... (this one should not appear)        (second is ignored, but third has its number)
note 3...                                     note 2...

I need that the variables don't execute, or something like that.


Solution

  • You can define a variabile in the jrxml that will do the counting for you. Assuming your field name is field1 of type String and you only like to increment counter when the field value is not null and not empty the ternary expression would be.

    <variable name="CntNonEmpty" class="java.lang.Integer" calculation="Sum">
      <variableExpression><![CDATA[$F{field1}!=null&&!$F{field1}.isEmpty()?1:0]]></variableExpression>
    </variable>
    

    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_1" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="149d63d1-922f-4b19-a2c4-ee1805064f26">
        <queryString>
            <![CDATA[]]>
        </queryString>
        <field name="field1" class="java.lang.String"/>
        <variable name="CntNonEmpty" class="java.lang.Integer" calculation="Sum">
            <variableExpression><![CDATA[$F{field1}!=null&&!$F{field1}.isEmpty()?1:0]]></variableExpression>
        </variable>
        <detail>
            <band height="32" splitType="Stretch">
                <printWhenExpression><![CDATA[$F{field1}!=null && !$F{field1}.isEmpty()]]></printWhenExpression>
                <textField>
                    <reportElement x="0" y="0" width="250" height="30" uuid="5f65fa92-ba25-4aba-a728-9b14e72572f4"></reportElement>
                    <textElement verticalAlignment="Middle"/>
                    <textFieldExpression><![CDATA[$F{field1} + " " + $V{CntNonEmpty}]]></textFieldExpression>
                </textField>
            </band>
        </detail>
    </jasperReport>
    

    Output

    With a datasource that contains 3 rows (2 row empty)

    textA
    
    textB
    

    result