Search code examples
javajasper-reports

Check if the value of field is at List which stored as report's parameter


I need to use an expression like in array function. I have a array parameter in JasperReports' report, Declared like this param1 and its class is java.util.List

I want to use an expression in print when expression.The expression should check the fields in that array. I tried this code {IN,$F{query_id},$P{param1}} but no use. $F{query_id} This the filed to check with array. Is there any way to check values in array?


Solution

  • You should use List.contains(Object) method.

    Sample

    <?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>
    

    Output result

    If the value is present at List (for example, 3 for List {1, 2, 3}) the result will be:

    JSS, textField is visible

    in other cases (value not found at List) the result in Jaspersoft Studio will be (the textField is hidden):

    JSS, textField is invisible

    In your case the valid expression will be like this:

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