Davi Bogo
Davi Bogo

Reputation: 149

How to add multiple expressions inside text field with conditional in jaspersoft?

I have a document that needs to be dynamic, so I added some textfields and I will pass as a parameter one number for what text it should be, I am using REPORT_PARAMETER_MAP for my footnotes, and I will need to put the footnotes after passing the text for the textfield. The problem is that Jaspersoft doesn't let me add more than one expression inside one conditional.

My textfield is doing this:

$P(param1) == 0 ? null : $P(param1) == 1 ? (
     "text for number one";
     $P{REPORT_PARAMETERS_MAP}.put("id_1", $V(footnote))
) : "text for other numbers"

For some reason jaspersoft doesn't understand what I am trying to do, and it doesn't accept ";", if I'm doing something wrong please explain to me.

Upvotes: 0

Views: 3487

Answers (1)

Petter Friberg
Petter Friberg

Reputation: 21710

You can't code using ; in a textField expression, all path's are expected to return a result (similar to excel formula). This type of expressions makes it almost impossibile to call void methods.

However, you can hack it some if method have return value

In your case $P{REPORT_PARAMETERS_MAP}.put("id_1", $V(footnote)) you are calling the put method on the Map api. As you can see it returns.

the previous value associated with key, or null if there was no mapping for key

Since it returns a value you can nest this call in your ternary expression, hence instead of just returning "text for number one"

 ($P{REPORT_PARAMETERS_MAP}.put("id_1", $V(footnote))==null || true)?"text for number one":""

As you can see this expression always evaluates to true, it will always return "text for number one" but it also put the value in the Map.

Simple jrxml example demonstrating technique

<?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="TestParams" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="f85bc6aa-5060-4485-bd99-bd5de1734ef1">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
    <parameter name="Param1" class="java.lang.Integer">
        <defaultValueExpression><![CDATA[1]]></defaultValueExpression>
    </parameter>
    <queryString>
        <![CDATA[]]>
    </queryString>
    <title>
        <band height="79" splitType="Stretch">
            <textField>
                <reportElement x="0" y="0" width="545" height="30" uuid="fc021fc5-85aa-4ea3-8372-3231fe4aeb84"/>
                <textFieldExpression><![CDATA[($P{Param1}!=null && $P{Param1}==1)?($P{REPORT_PARAMETERS_MAP}.put("Hi","Hello world")!=null || true)?"We put Hi!":"":"Param1 was not 1"]]></textFieldExpression>
            </textField>
        </band>
    </title>
    <summary>
        <band height="42" splitType="Stretch">
            <textField>
                <reportElement x="0" y="0" width="544" height="30" uuid="56d3ab2d-19c9-4314-84b3-e6103e54446f"/>
                <textFieldExpression><![CDATA[$P{REPORT_PARAMETERS_MAP}.get("Hi")]]></textFieldExpression>
            </textField>
        </band>
    </summary>
</jasperReport>

Upvotes: 1

Related Questions