clark_6 Posted February 25, 2022 Share Posted February 25, 2022 Hello,I am new to jaspersoft and a need a quick solution to a report.How do I express this following expression in Jaspersoftif field 1 is not empty then display field 1 else display field 2.Thankyou Link to comment Share on other sites More sharing options...
bigalex Posted February 25, 2022 Share Posted February 25, 2022 Use Print When Expression. Please see the example below.Assuming that VAR1 in Field 1 and VAR2 in Field 2, then add expression to Print When Expression: $F{VAR1}!=null will display field 1 when it is not empty. The same for field 2.<?xml version="1.0" encoding="UTF-8"?><!-- Created with Jaspersoft Studio version 6.16.0.final using JasperReports Library version 6.16.0-48579d909b7943b64690c65c71e07e0b80981928 --><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="2fields" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="8d2657b1-69a0-45dc-95f3-d1f76163e412"> <parameter name="Field1" class="java.lang.String"/> <parameter name="Field2" class="java.lang.String"/> <queryString> <![CDATA[]]> </queryString> <background> <band splitType="Stretch"/> </background> <pageHeader> <band height="81" splitType="Stretch"> <textField evaluationTime="Report"> <reportElement mode="Opaque" x="10" y="8" width="460" height="30" forecolor="#FFFFFF" backcolor="#FA100C" uuid="e7344090-e068-42b3-a60e-1199c653d487"> <printWhenExpression><![CDATA[$P{Field1}!=null]]></printWhenExpression> </reportElement> </textField> <textField evaluationTime="Report"> <reportElement mode="Opaque" x="10" y="40" width="460" height="30" forecolor="#050505" backcolor="#07FAE6" uuid="c81b9400-78e0-4840-964a-ae69d67644c3"> <printWhenExpression><![CDATA[$P{Field2}!=null]]></printWhenExpression> </reportElement> </textField> </band> </pageHeader></jasperReport> Link to comment Share on other sites More sharing options...
Tom C Posted February 27, 2022 Share Posted February 27, 2022 JasperReports (JRL) is a Java application therefore you really need to know how the Java Ternary Operator works to help code the report field expression in your report design templae JRXML file. Please refer to:https://careerkarma.com/blog/java-ternary-operator/---------------------------------------------------In your case, the requirement is:"if field 1 is not empty then display field 1 else display field 2".Suppose you have field 1 and field 2 from the SQL query result dataset$F{text_1} and $F{text_2} The text field expression in the report template would be: <textField> <reportElement ......></reportElement> <textFieldExpression><![CDATA[$F{text_1}==null||$F{text_1}.trim().isEmpty()?$F{text_2}:$F{text_1}]]></textFieldExpression> </textField> Hope this helps. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now