Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
795 views
in Technique[技术] by (71.8m points)

jasper reports - doing comparison if else in JasperReports

I want to do a comparison such as:

if <field> == 0 then "-"

Can somebody tell me the syntax using JasperReports?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...