doing comparison if else in JasperReports

二次信任 提交于 2019-11-27 05:14:42

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)

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"
)
harun ugur

Use if-else condition:

  1. if customer name is null write '-' (absent), else write customer name.

Be careful of your field data type!

<textFieldExpression class="java.lang.String">
  <![CDATA[
    $F{CustomerName} == null ? '-' : $F{CustomerName}
  ]]>
</textFieldExpression>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!