if-else-if-else in textfield expression [duplicate]
Asked Answered
C

5

16

I have to show columns: - send - receive - cancelled

In a report column by checking a value from DB which is "Status". So if Status equals 1 then send,=2 receive , = 3 cancelled.

The textfield expression in jasper report ( ? a:b) can only take one condition, how do i give multiple conditions ? something like if-else ladder ?

Chamfer answered 13/1, 2011 at 7:17 Comment(0)
L
28

You can use a nested ternary statement to achieve this but it's messy.

For example:

(i == 1) ? "Send" : ((i == 2)? "Received" : "Cancelled");
Libbie answered 13/1, 2011 at 11:44 Comment(3)
I also did this few times, and it's ok for up to three conditions... but who would decode it if you write 5 or more conditions?Desiderative
thanks,me too did this.. but like @Desiderative said.. 3 is max.Chamfer
This answer results in code-smell for sure. @Sean is right: Do any logic in your bean, passing the result to the report.Micheal
B
7

you can use replace() method, but you must change your field's class to String if it is not, EX:

$F{f_phone_type}.replace("0","Phone/ATA").replace("1","Gateway").replace("2","SIPTrunk")
Bummalo answered 11/4, 2011 at 2:41 Comment(0)
P
4

Make the status field a parameter that is passed from your bean. That way you can do whatever processing you need in your bean, assign the result to the parameter variable, and pass it into your report.

Pren answered 24/1, 2011 at 6:11 Comment(1)
thank you.. setting in the bean might be a viable solution rather than giving expression in the report itself.Chamfer
B
4

My preferred method is to create a parameter with the substitutions in the form of a HashMap, when you have a large group of substations or if they can change.

You can either pass the substitutions at runtime or set a default value. The benefit being that you can update the map without recompiling your report.

For example if you were to have a parameter named "risk_types" (Using the default language as groovy) you'd set the parameter default value to something like

[1: "HIGH RISK", 2: "LOW RISK"]

In your code, you'd have your text field expression as (where risk_type is the lookup field from the database):

$P{risk_types}.get($F{risk_type})

For items missing in the map, you'd get a null value, I extend the text field expression to be:

$P{risk_types}.get($F{risk_type}) ?: "UNDEFINED"
Batchelder answered 24/6, 2013 at 5:3 Comment(0)
W
1

Boolean.valueOf(!($P{accWise}.equals("AC" ) && $F{RQAC_ACCREJ}.equals("R"))) is useful to give multiple conditions in ireport properties.

Wilderness answered 7/1, 2014 at 6:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.