Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

doing comparison if else in JasperReports

Tags:

I want to do a comparison such as:

if <field> == 0 then "-" 

Can somebody tell me the syntax using JasperReports?

like image 614
Chi Avatar asked Dec 14 '10 08:12

Chi


People also ask

How do you write if-else condition in Jasper report?

Jasper Reports doesn't support if-else statements when defining variable expressions. Instead you can use the ternary operators {cond} ? {statement 1} : {statement 2}. You can nest this operator inside a Java expression to obtain the desired output based on multiple conditions.

How do I compare strings in Jasper report?

If you want to compare string fields then use . equals() function instead of == operator. In Java, the == operator compares if the objects are the same instance while . equals() compares the state of object.

How do you write expressions in JasperReports?

JasperReports doesn't support if-else statements when defining variable expressions. Instead, you can use the ternary operators {cond} ? {statement 1} : {statement 2}. This operator can be nested inside a Java expression to obtain the desired output based on multiple conditions.

What is the difference between Jasper and Jrxml?

jrxml is a human readable XML file that contains the report template i.e. report structure and its formatting rules. . jasper is the compiled report template i.e. compiled . jrxml file.


2 Answers

iReport (JasperReports) uses a Ternary operator. For example, consider the following logic:

IF boolean condition THEN   execute true code ELSE   execute false code END IF 

Using a ternary operator, this becomes:

boolean condition ? execute true code : execute false code 

When using a variable with the following expression:

$F{column_value}.intValue() == 42 ? "Life, Universe, Everything" : "Naught" 

Then the variable's value would be "Life, Universe, Everything" if, and only if, the integer value of $F{column_value} is equal to 42.

Where things get a little obtuse is when you have to have nested conditions. For these, put the nested conditions in parenthesis and on a separate line:

condition1 ?   (condition2 ? true_code2 : false_code2) :   false_code1 

So when you need to do many of them:

condition1 ?   (condition2 ?     (condition3 ? true_code3 : false_code3) :     false_code2) :   (condition4 ? true_code4 : false_code4) 
like image 93
Dave Jarvis Avatar answered Oct 14 '22 08:10

Dave Jarvis


example of expression in ireport:

(     $F{foo} == 0 ?     "Planned" :     $F{foo} == 1 ?     "Reserved" :     $F{foo} == 2 ?     "Canceled" :     $F{foo} == 3 ?     "Absent" :     $F{foo} == 4 ?     "Complete" :     "Unknown" ) 
like image 32
Michel Avatar answered Oct 14 '22 08:10

Michel