Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing parameters from main report to subreport in Jasper

Tags:

I have a JasperReport where I am passing the report Date from a Main Report to a sub report. This works fine. I also have another where I am passing the same parameter to a sub report that has multiple SubReports. When I preview it in Ireports all the pages for it appear blank which means the sub-sub reports are being called but the report Date is not being passed as all the sub-sub report SQL are conditioned on the report Date. How can I get the report Date field to the sub sub reports? When I preview the SubReport and type in the report Date all the report dates work all four pages are populated with the correct data.

like image 739
user1898250 Avatar asked Dec 12 '12 15:12

user1898250


People also ask

How do you pass a value from main report to subreport?

2 Answers:Click on the subreport element in the main report and on the right side you will edit parameters button . select that and map the values you want to pass it from the main report to the subreport. Vote up! Vote down!

How do you pass a field to subreport in Jasper report?

Create a subreport parameter (for the sub-report from the main report) called "paramA" and pass it the value you wish by setting the report expression as needed. If the value is in a field try using $F{myField} as the expression, if its a parameter that was passed to the main report, use $P{myParam} etc.

How pass parameter from main report to table in Jasper report?

To pass parameters from the Main report to the datasets, the parameters have to have the same name. That is, if the name of the parameter in the main report is 'Region', then the Datasets which need this parameter will have to have a parameter with an identical name.


2 Answers

Follow the steps below to pass parameters into subreports:

  1. Create main report parameter, such as DATE_PARAM.
  2. Open sub report and Create a parameter with the same name and the same type.
  3. Go back to main report
  4. Right-click on sub report, select properties
  5. Choose parameter
  6. Add parameter from main report to sub report with parameter name same parameter name

The parameter is passed from the main report to the subreport.

like image 195
Ahmed Salem Avatar answered Sep 18 '22 16:09

Ahmed Salem


My guess is they are using a default value.

Assuming the name of the Parameter in the Main Report is TEST_DATE and the name in the Sub-report is TEST_DATE2 then you need to add the following in between the opening and closing subreport elements in the XML:

<subreportParameter name="TEST_DATE2">     <subreportParameterExpression><![CDATA[$P{TEST_DATE}]]></subreportParameterExpression> </subreportParameter> 

For the sake of completeness, here is an example that has a main report and a subreport, that should work by just changing the value for SUBREPORT_DIR to point to where you have them placed.

report1.jrxml

<?xml version="1.0" encoding="UTF-8"?> <jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="report1" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="7e2bc622-d768-437e-8c33-fc777bc06f8c">     <property name="ireport.zoom" value="1.0"/>     <property name="ireport.x" value="0"/>     <property name="ireport.y" value="0"/>     <parameter name="TEST_DATE" class="java.util.Date"/>     <parameter name="SUBREPORT_DIR" class="java.lang.String" isForPrompting="false">         <defaultValueExpression><![CDATA["C:\\Reports\\"]]></defaultValueExpression>     </parameter>     <pageHeader>         <band height="83" splitType="Stretch">             <textField>                 <reportElement uuid="4a2cf434-4473-48db-a89f-17a19d25cc4c" x="0" y="0" width="100" height="20"/>                 <textElement/>                 <textFieldExpression><![CDATA[$P{TEST_DATE}]]></textFieldExpression>             </textField>             <subreport>                 <reportElement uuid="54c02e96-6d47-49db-9b9c-58e1dd153242" x="0" y="30" width="200" height="35"/>                 <subreportParameter name="TEST_DATE2">                     <subreportParameterExpression><![CDATA[$P{TEST_DATE}]]></subreportParameterExpression>                 </subreportParameter>                 <connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>                 <subreportExpression><![CDATA[$P{SUBREPORT_DIR} + "report1_subreport1.jasper"]]></subreportExpression>             </subreport>         </band>     </pageHeader> </jasperReport> 

report1_subreport1.jrxml

<?xml version="1.0" encoding="UTF-8"?> <jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="report1_subreport1" language="groovy" pageWidth="200" pageHeight="35" whenNoDataType="AllSectionsNoDetail" columnWidth="200" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0" uuid="3cedac90-63cb-43cb-9d0f-e401543e65dd">     <property name="ireport.zoom" value="1.0"/>     <property name="ireport.x" value="0"/>     <property name="ireport.y" value="0"/>     <parameter name="TEST_DATE2" class="java.util.Date" isForPrompting="false"/>     <pageHeader>         <band height="35" splitType="Stretch">             <textField>                 <reportElement uuid="ca7f3da6-79f0-4d95-92db-6c5dbf777df9" x="0" y="15" width="100" height="20"/>                 <textElement/>                 <textFieldExpression><![CDATA[$P{TEST_DATE2}]]></textFieldExpression>             </textField>         </band>     </pageHeader> </jasperReport> 
like image 42
Jacob Schoen Avatar answered Sep 22 '22 16:09

Jacob Schoen