yayayokoho3
yayayokoho3

Reputation: 1199

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

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?

Upvotes: 12

Views: 22204

Answers (6)

Petter Friberg
Petter Friberg

Reputation: 21710

The correct way (jasper report v.5/v.6) to return values from a component using subDataset is to use variables, define variables in both main report and in subDataset.

Example (return record count of table to main report)

  1. In main report define a variable

    <variable name="TABLE_COUNT" class="java.lang.Integer" resetType="None">
       <initialValueExpression><![CDATA[0]]></initialValueExpression>
    </variable>
    
  2. In subdataset define a variable (in example a build in variable will be used $V{REPORT_COUNT}).

  3. In datasetRun indicate which subDataset variable (fromVariable) should be return to which main report variable (toVariable)

    <datasetRun subDataset="tableData" uuid="fa5df3de-f4c5-4bfc-8274-bd064e8b81e6">
       <connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
       <returnValue fromVariable="REPORT_COUNT" toVariable="TABLE_COUNT"/>
    </datasetRun>
    

The TABLE_COUNT variable can then be used in the main report, just remember to set correct evaluationTime

Display the value (in main report)

<textField evaluationTime="Report">
    <reportElement x="0" y="0" width="100" height="20" uuid="d67ddb3e-b0cc-4fae-9e05-f40eb0f7e059"/>
    <textFieldExpression><![CDATA[$V{TABLE_COUNT}]]></textFieldExpression>
</textField>

Upvotes: 12

Dark Army
Dark Army

Reputation: 268

For Map Type it is possible for rest any type returning value from table-set to the main report is not possible.

Although one can use sub-report as there is options for returning values.If possible try to use sub-reports in place of Table.

Upvotes: 0

gpanzl
gpanzl

Reputation: 41

Now you can return value from from the dataset of a table to the main report.

On jasper studio 6.0.1,I found a property named dataset of table, it has return values set. But get into the set , I found the form "configure the return values" is wrong, jasper studio get wrong with "from variable to variable" , stuio reverse the two variable. "from variable" is table's local variable, "to value" is main report variable.You must manaualedit in source , and remove "incrementerFactoryClass=""".

I test ,it is ok .The return value is printed on main report.

next is a piece of my jrxml.

<jr:table xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd">
					<datasetRun subDataset="workscore" uuid="307278bc-db98-4de2-9b50-dea5dc69b496">
						<connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
						<returnValue fromVariable="returnscore" toVariable="workscore"/>
					</datasetRun>

Upvotes: 0

Andrey Frunt
Andrey Frunt

Reputation: 111

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!

Upvotes: 11

Gopinagh.R
Gopinagh.R

Reputation: 4916

You will not be able to return a variable from the dataset of a table to the main report , whereas the vice versa is possible. An Alternative can be as suggested by mdahlman. Use a Sub-Report instead.

Upvotes: 0

mdahlman
mdahlman

Reputation: 9390

It sounds reasonable... but I'm not sure whether it's possible. It could be a useful enhancement request.

An alternative is to use a subreport. Anything possible in a Table is possible in a Subreport. Subreports have Return Values, and that will let you pass back the information you need.

Upvotes: 1

Related Questions