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?
If you want to get some data from the table dataset, you actually can do this. I found a dirty trick to achieve this.
Create VARIABLE
variableMapNameof typejava.util.Mapin main report and initialize it with expressionnew java.util.HashMap()Create PARAMETER
parameterMapNameof typejava.util.Mapwithin table datasetLink 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)
Create variable
putResultof typejava.lang.Stringin 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!
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)
In main report define a variable
<variable name="TABLE_COUNT" class="java.lang.Integer" resetType="None"> <initialValueExpression><![CDATA[0]]></initialValueExpression> </variable>In
subdatasetdefine a variable (in example a build in variable will be used$V{REPORT_COUNT}).In
datasetRunindicate whichsubDatasetvariable (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>
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.
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.
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>
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.
来源:https://stackoverflow.com/questions/10394401/how-to-return-value-from-tables-datasource-to-main-report-in-ireport