How to return value from table's DataSource to main report in iReport?

yayayokoho3 picture yayayokoho3 · May 1, 2012 · Viewed 18k times · Source

I have a table in my iReport which naturally has its dataset and I have a variable, that is defined and initialized in the table's dataset return a value (which definitely does within scope of table, not outside it) which I want to use in my main report which holds the table.

How can I do that or any alternatives?

Answer

Andrey Frunt picture Andrey Frunt · Dec 19, 2013

If you want to get some data from the table dataset, you actually can do this. I found a dirty trick to achieve this.

  1. Create VARIABLE variableMapName of type java.util.Map in main report and initialize it with expression new java.util.HashMap()

  2. Create PARAMETER parameterMapName of type java.util.Map within table dataset

  3. Link dataset PARAMETER with variable from main report using "Edit table datasource -> Parameters -> Add -> $P{parameterMapName} = $V{variableMapName}" (right click on the table in main report template)

  4. Create variable putResult of type java.lang.String in table datasource. Expression for this variable will looks like this

$P{parameterMapName}.put("KEY", $F{fieldYouWantReturn}) + $P{parameterMapName}.put("KEY2", $F{otherFieldYouWantReturn})

Now you have an ability to use the data from table datasource in main report by using $V{variableMapName}.get("KEY")

It is really a dirty hack, but sometimes you have no other way to do something. Thanks!