25

我想做一个比较,例如:

if <field> == 0 then "-"

有人可以告诉我使用 JasperReports 的语法吗?

4

3 回答 3

40

iReport (JasperReports) 使用三元运算符。例如,考虑以下逻辑:

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

使用三元运算符,这变为:

boolean condition ? execute true code : execute false code

使用具有以下表达式的变量时:

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

那么当且仅当 的整数值$F{column_value}等于 42 时,变量的值将是“Life, Universe, Everything”。

当你必须有嵌套条件时,事情变得有点迟钝。对于这些,将嵌套条件放在括号中并单独一行:

condition1 ?
  (condition2 ? true_code2 : false_code2) :
  false_code1

因此,当您需要执行其中许多操作时:

condition1 ?
  (condition2 ?
    (condition3 ? true_code3 : false_code3) :
    false_code2) :
  (condition4 ? true_code4 : false_code4)
于 2011-01-06T19:37:16.563 回答
23

ireport 中的表达式示例:

(
    $F{foo} == 0 ?
    "Planned" :
    $F{foo} == 1 ?
    "Reserved" :
    $F{foo} == 2 ?
    "Canceled" :
    $F{foo} == 3 ?
    "Absent" :
    $F{foo} == 4 ?
    "Complete" :
    "Unknown"
)
于 2011-10-13T07:31:25.720 回答
2

使用 if-else 条件:

  1. 如果客户名称为空,则写入“-”(不存在),否则写入客户名称。

请注意您的字段数据类型!

<textFieldExpression class="java.lang.String">
  <![CDATA[
    $F{CustomerName} == null ? '-' : $F{CustomerName}
  ]]>
</textFieldExpression>
于 2017-07-14T08:42:52.657 回答