检查字段的值是否为存储为报告参数的List

时间:2017-02-16 05:44:34

标签: java jasper-reports

我需要使用类似in array函数的表达式。 我在JasperReports的报告中有一个数组参数,声明为param1,其类为java.util.List

我想在print when expression中使用表达式。表达式应该检查该数组中的字段。我尝试了这段代码{IN,$F{query_id},$P{param1}},但没有用。 $F{query_id}这是用数组检查的字段。有没有办法检查数组中的值?

1 个答案:

答案 0 :(得分:2)

您应该使用 List.contains(Object) 方法。

示例

<?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="List contains sample" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
    <parameter name="list" class="java.util.List">
        <defaultValueExpression><![CDATA[Arrays.asList(1, 2, 3)]]></defaultValueExpression>
    </parameter>
    <parameter name="valueToFind" class="java.lang.Integer">
        <defaultValueExpression><![CDATA[5]]></defaultValueExpression>
    </parameter>
    <title>
        <band height="79" splitType="Stretch">
            <textField>
                <reportElement x="180" y="10" width="200" height="30">
                    <printWhenExpression><![CDATA[$P{list}.contains($P{valueToFind})]]></printWhenExpression>
                </reportElement>
                <textFieldExpression><![CDATA["I'm visible if value at list"]]></textFieldExpression>
            </textField>
        </band>
    </title>
</jasperReport>

输出结果

如果值出现在 List (例如,列表{1,2,3}中的3),结果将为:

JSS, textField is visible

在其他情况下(在List中找不到值) Jaspersoft Studio 中的结果将是( textField 被隐藏):

JSS, textField is invisible

在您的情况下,有效表达式将如下所示:

<printWhenExpression><![CDATA[$P{param1}.contains($F{query_id})]]></printWhenExpression>
相关问题