How to use printWhenExpression in JasperReports

2019-02-16 08:39发布

Can somebody tell me how to use the printWhenExpression of JasperReports?

4条回答
我只想做你的唯一
2楼-- · 2019-02-16 09:06

The other poster has done a good job of explaining the technical details of how to use it, so I'll try and explain the circumstances in which one might find it useful.

Basically, it allows you to show or hide the contents of a cell based on a boolean expression. For example, you might want to show a person's name only if that person is over 18, then in the name field, using a printwhenexpression like:

$F{age} >= 18
查看更多
看我几分像从前
3楼-- · 2019-02-16 09:08

You can also use the static method "Boolean.valueOf(boolean b)". It does exactly the same logic as "($F{mesure}.startsWith("PH") ? Boolean.TRUE:Boolean.FALSE)" and good rule of thumb is don't recreate the wheel.

Boolean.valueOf($F{mesure}.startsWith("PH"))

Boolean.valueOf($F{userfd4}).equals("1"))

查看更多
虎瘦雄心在
4楼-- · 2019-02-16 09:15

$P{mesure}.equals("200") should work without any additional details. It will print if the condition is true

查看更多
Root(大扎)
5楼-- · 2019-02-16 09:18

Do you have an error relative to boolean ?

Because you need to use Boolean instead of the primitive type.

So:

$F{mesure} != "PH"
($F{userfd4}).equals("1") ? true : false 

would give cannot cast from boolean to Boolean.

( $F{mesure}.startsWith("PH") ? Boolean.TRUE:Boolean.FALSE ) 
($F{userfd4}).equals("1") ? Boolean.TRUE : Boolean.FALSE  

would be correct.

See also this example


Update Nov 2015 (7 years later)

Petter Friberg points out in the comments:

In jasper report 6.0 this is not need:
You can return either boolean or Boolean a simple expression like $F{fieldName}.equals("hello") will work.

查看更多
登录 后发表回答