How to change the value of a variable on demand in JasperReports

I work with iReport. Here is the situation:

I have some parameters called branchID, spType .

I created one variable called branchName (string) , which defaults to null.

I need to do to change / change the value of the variable branchName depending on the values โ€‹โ€‹of branchID and spType

How can I do this using JasperReports using If Else. Is this something with branchName variable branchName ? Any help / suggestion would be much appreciated.

+4
source share
2 answers

Expressions The scripting language in Jasper's reports is your friend!

What I can understand from your query is that you want to calculate the value of a variable based on the values โ€‹โ€‹of two parameters. In short, you want to do something like: -

Simple if-else

 If (branchId == 1 && spType == "abc") branchName = "Xyz1"; else branchName = "Xyz2"; 

In the Jasper scripting language, the specified expression can be written as: -

 ($P{branchId}==1 && $P{spType} != null && $P{spType}.equals("abc"))?"Xyz1":"Xyz2" 

Nested If-else

 If (branchId == 1 && spType == "abc") branchName = "Xyz1"; else if (branchId = 2 && spType == "def") branchName = "Xyz2"; else branchName = "Xyz3"; 

Expression of expression:

 ( $P{branchId} ==1 && $P{spType} !=null && $P{spType}.equals("abc") ) ? "Xyz1" : (($P{branchId} == 2 && $P{spType} != null && $P{spType}.equals("def")) ? "Xyz2" : "Xyz3"); 

See this lesson for more details: http://www.tutorialspoint.com/jasper_reports/jasper_report_expression.htm

Here is a similar stackoverflow question as well: do a comparison if else in JasperReports

For the basic concept, page number. 10 of this pdf: http://jasperreports.sourceforge.net/JasperReports-Ultimate-Guide-3.pdf

+6
source

Support for iReport scripts using expressions .

You can right-click on a report item and click "Edit Expression" to open the expression editor.

Some useful notes in this answer .

0
source

Source: https://habr.com/ru/post/1496959/


All Articles