如何在 jaspersoft 中使用条件在文本字段中添加多个表达式?

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

我有一个需要动态的文档,所以我添加了一些文本字段,我将作为参数传递一个数字来表示它应该是什么文本,我使用 REPORT_PARAMETER_MAP 作为我的脚注,我将需要在传递文本字段的文本后放置脚注。 问题是 Jaspersoft 不允许我在一个条件中添加多个表达式。

我的文本框是这样做的:

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

出于某种原因,jaspersoft 不理解我要做什么,它不接受“;”,如果我做错了什么,请向我解释。

您不能在文本字段表达式中使用 ; 进行编码,所有路径都应 return 结果(类似于 excel 公式)。这种类型的表达式几乎不可能调用 void 方法。

但是,如果方法具有 return 值

,您可以破解它

在您的情况下 $P{REPORT_PARAMETERS_MAP}.put("id_1", $V(footnote)) 您正在 Map api 上调用 put 方法。如您所见 returns.

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

由于它 return 是一个值,您可以将此调用嵌套在您的三元表达式中,因此而不仅仅是 returning "text for number one"

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

如您所见,此表达式的计算结果始终为 true,它始终为 return "text for number one",但它也 put 映射中的值。

演示技术的简单 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="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>