Jump to content
JasperReports 7.0 is now available ×

Sub Reports


pnvswamy

Recommended Posts

  • Replies 27
  • Created
  • Last Reply

Top Posters In This Topic

subreports are used where you want to represent data that is child to the data in the parent report. You could show data that is a reference data which may not any relation. You can have as many levels of subreports. You can pass parameters to the subreport jrxml from the subreport parameter tab in the subreport element. The same parameter name is to be specified in the subreport jrxml in order that the value is passed sucessfully.
Link to comment
Share on other sites

I understood the usage of subreports.I developed the main report and sub report using  IREPORT tool.In i report i am directly connecting the database.So both reports are working fine.

My problem came when i am using the application.

In the application i am using JRBeanCollectionDatasource for the Report data as a ArrayList of BeanClasses.

Could you please help me how to pass parameter to sub report from the main report in the application.

Link to comment
Share on other sites

hi

I am generating jasper reports with out writing any query string in .jrxml files.I am using JRBeanCollectionDataSource.

I am facing problems in sub reports.

main report having the fields like empCode,Name only.

These fields are getting throguh EmpBeans as an arrylist to passing a parameter to JRBeanCollectionDataSource.

Based on empcode i am getting the employee projects done have to shown using sub reports.

These projects are under another ProjectBean.

How can i fill the sub report based on the empCode.Please help me.

If i use query strings in jrxml there is no problem in ireport.

Link to comment
Share on other sites

Class MainReport{

List<EmpDetail>

List <ProjectDetail>

}

Class EmpDetail{

empCode,Name

}

Class ProjectDetail{

empCode, projectDetails

}

Your main class should look like this.

You have beancollection of MainReport only.

The mainReport will have two fields automatically created as List. Once you done that change the subreport datasource as JavaBeancollectionSource(NameofListField). This way you can achieve very complex reports. Your subreport just needs to have the fields from the respective beans. It is difficult to write all in message but i have written enough for your understanding.

 

 

Link to comment
Share on other sites

In this employeeList is the arryList i am fiiling the main report.In that it is having EmpDetail bean class.For each it has empCode ,for ecah code i want Project details to fill as arrayList.In Main report i am passing parameter as $P{empCode}

where i have to check the condition and where i ahave to fill the sub report as arryList based on EmpCode.

 

Thank you,

My problem is for the main report i am using EmpDetail bean class

and for sub report i am using ProjectDetails bean class.

first i am filling the main report like this  :

---------------------------------------------------------------------new File(getServlet().getServletContext().getRealPath("reports/PaEmployeeDetailReport.jrxml"));null; new java.util.HashMap();//parameters.put("subReport",jasperReport1);

JRBeanCollectionDataSource ds =

JRBeanCollectionDataSource(employeeList);

JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, ds );

new

-----------------------------------------

File fileSrc =

File destFile =

JasperDesign jasperDesign = JRXmlLoader.load(fileSrc);

JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);

 

java.util.Map parameters =

 

 

Link to comment
Share on other sites

You might have to change the implementation slightly.

You dont have to populate sub report as arryList based on EmpCode. All the data will be populated right before you generate the report. So you have to organise your data in the following form.

Class EmpDetail {

String EmpCode

List ProjectDetailsClass

}

So your first main report JRBeanCollectionDataSource is JrBeanCollectionDataSource(List<EmpDetail>)

Rest of the population is just iReport Configuration of subreport. You subreport should have fields for ProjectDetailsClass.

Change the subreport element properties to following:-

At the subreport tab select "use DataSource Expression"

Here enter "new JrBeanCollectionDataSource(List<ProjectDetail>)" as the datasource expression.

I hope i am clear now.

 

 

Link to comment
Share on other sites

I am filling the main report like this:
----------------------------------------
File fileSrc = new File(getServlet().getServletContext().getRealPath("reports/PaEmployeeDetailReport.jrxml"));
File destFile = null;
JasperDesign jasperDesign = JRXmlLoader.load(fileSrc);
JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
  
java.util.Map parameters = new java.util.HashMap();
  
/*employeeList is the EmpDetail List*/
JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(employeeList);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, ds );
------------------------------------------------------------------------------
i want to fill sub report also
Please help me......

Link to comment
Share on other sites

I am filling the main report like this:
----------------------------------------
File fileSrc = new File(getServlet().getServletContext().getRealPath("reports/PaEmployeeDetailReport.jrxml"));
File destFile = null;
JasperDesign jasperDesign = JRXmlLoader.load(fileSrc);
JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
  
java.util.Map parameters = new java.util.HashMap();
  
/*employeeList is the EmpDetail List*/
JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(employeeList);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, ds );
------------------------------------------------------------------------------
i want to fill sub report also
Please help me......

 

Please see the files:

MainReport : PaEmployeeDetailReport.jrxml

sub report  :  Employee_subreport1.jrxml

Method in java class is :

generateReport()

Link to comment
Share on other sites

generateMethod looks like this:new PaReportsHelper();"reports/Employee_subreport1.jrxml");"reports/Employee_subreport1.jasper");out.println("List Size:"+employeeList.size());if(employeeList.size() == 0 ){new ActionMessage("PA.PaEmpReportsByStatusAction.generateReport.noData");"noData", noData);return mapping.findForward("noRecords");try{

// JasperCompileManager.compileReportToFile(getServlet().getServletContext().getRealPath("reports/PaEmployeeDetailReport.jrxml"), getServlet().getServletContext().getRealPath("reports/PaEmployeeDetailReport.jasper"));

JasperDesign jasperDesign1 = JRXmlLoader.load(subFileSrc);

JasperReport jasperReport1 = JasperCompileManager.compileReport(jasperDesign1);

JasperCompileManager.compileReportToFile(subFileSrc,subFileDest );

List projects = reportsHelper.employeeProjectDetails(

 

File fileSrc =

File destFile =

JasperDesign jasperDesign = JRXmlLoader.load(fileSrc);

JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);

 

java.util.Map parameters =

 

parameters.put(

parameters.put(

JRBeanCollectionDataSource ds =

JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, ds );

JRExporter jrExporter =

new Integer(5));new File(getServlet().getServletContext().getRealPath("reports/PaEmployeeDetailReport.jrxml"));null; new java.util.HashMap();"subReport",jasperReport1);"subds",new JRBeanCollectionDataSource(projects));new JRBeanCollectionDataSource(employeeList);null;

 

PaReportsHelper reportsHelper =

String subFileSrc =getServlet().getServletContext().getRealPath(

String subFileDest =getServlet().getServletContext().getRealPath(

List employeeList = reportsHelper.getEmployeeDetails123(department,employee,status,type,loginUnitId,subFileSrc,subFileDest);

System.

 

 

ActionMessage noData =

messages.add(

saveMessages(request, messages);

 

}

 

 

 

Link to comment
Share on other sites

the generateReport() method looks like this{

PaReportsHelper reportsHelper =

new PaReportsHelper();"reports/Employee_subreport1.jrxml");"reports/Employee_subreport1.jasper");out.println("List Size:"+employeeList.size());if(employeeList.size() == 0 ){new ActionMessage("PA.PaEmpReportsByStatusAction.generateReport.noData");"noData", noData);return mapping.findForward("noRecords");try{

// JasperCompileManager.compileReportToFile(getServlet().getServletContext().getRealPath("reports/PaEmployeeDetailReport.jrxml"), getServlet().getServletContext().getRealPath("reports/PaEmployeeDetailReport.jasper"));

JasperDesign jasperDesign1 = JRXmlLoader.load(subFileSrc);

JasperReport jasperReport1 = JasperCompileManager.compileReport(jasperDesign1);

JasperCompileManager.compileReportToFile(subFileSrc,subFileDest );

List projects = reportsHelper.employeeProjectDetails(

 

File fileSrc =

File destFile =

JasperDesign jasperDesign = JRXmlLoader.load(fileSrc);

JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);

 

java.util.Map parameters =

 

parameters.put(

parameters.put(

JRBeanCollectionDataSource ds =

JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, ds );

new Integer(5));new File(getServlet().getServletContext().getRealPath("reports/PaEmployeeDetailReport.jrxml"));null; new java.util.HashMap();"subReport",jasperReport1);"subds",new JRBeanCollectionDataSource(projects));new JRBeanCollectionDataSource(employeeList);

}

String subFileSrc =getServlet().getServletContext().getRealPath(

String subFileDest =getServlet().getServletContext().getRealPath(

List employeeList = reportsHelper.getEmployeeDetails123(department,employee,status,type,loginUnitId,subFileSrc,subFileDest);

System.

 

 

ActionMessage noData =

messages.add(

saveMessages(request, messages);

 

}

 

 

 

Link to comment
Share on other sites

hi,

I am having the field type ArrayList in employee Bean.

I want to show that list in Report.

Ex : i am setting the projectDetails as a ArrayList in Bean.

And i configured the <field name="projectDetails" class="java.util.ArrayList"/>

then i am getting the exception like --->

Could you please help me how to represent arraylist type Data in .jrxml file.

Error retrieving field value from bean : projectDetails

Link to comment
Share on other sites

Hi,

i given like this in .jrxml file

<

 

field name="projDetails" class="java.util.List"

/>

and in sub report part

<subreport>
 <reportElement x="9" y="665" width="502" height="20"/>
 <dataSourceExpression><![CDATA[new JRBeanCollectionDataSource($F{projDetails})]]></dataSourceExpression>
 <subreportExpression class="net.sf.jasperreports.engine.JasperReport"><![CDATA[$P{subReport}]]></subreportExpression>
    
</subreport>

then it was giving the error like

Errors were encountered when compiling report expressions class file:

1. JRBeanCollectionDataSource cannot be resolved or is not a type

value = (net.sf.jasperreports.engine.JRDataSource)(new JRBeanCollectionDataSource(((java.util.List)field_projDetails.getValue())));/*$JR_EXPR_ID=39$*/

<------------------------>

2. JRBeanCollectionDataSource cannot be resolved or is not a type

value = (net.sf.jasperreports.engine.JRDataSource)(new JRBeanCollectionDataSource(((java.util.List)field_projDetails.getOldValue())));/*$JR_EXPR_ID=39$*/

<------------------------>

3. JRBeanCollectionDataSource cannot be resolved or is not a type

value = (net.sf.jasperreports.engine.JRDataSource)(new JRBeanCollectionDataSource(((java.util.List)field_projDetails.getValue())));/*$JR_EXPR_ID=39$*/

<------------------------>

3 errors

 

Please help me 

Link to comment
Share on other sites

And I have one more doubt.Is there any way to execute the group in jrxml file depending on the Parameter.

Eg: In java i have parameters like groupByEmp,GroupByDept like this

Depending on the Java Parameter i have to execute the Group of that parameter and remaining group will not execute.

Is it possible.

Link to comment
Share on other sites

  • 2 weeks later...
  • 1 year later...

Hello,

I don't understand the solution gave to solve the problem :

Errors were encountered when compiling report expressions class file:

1. JRBeanCollectionDataSource cannot be resolved or is not a type

value = (net.sf.jasperreports.engine.JRDataSource)(new JRBeanCollectionDataSource(((java.util.List)field_projDetails.getValue())));/*$JR_EXPR_ID=39$*/

<------------------------>

2. JRBeanCollectionDataSource cannot be resolved or is not a type

value = (net.sf.jasperreports.engine.JRDataSource)(new JRBeanCollectionDataSource(((java.util.List)field_projDetails.getOldValue())));/*$JR_EXPR_ID=39$*/

<------------------------>

3. JRBeanCollectionDataSource cannot be resolved or is not a type

value = (net.sf.jasperreports.engine.JRDataSource)(new JRBeanCollectionDataSource(((java.util.List)field_projDetails.getValue())));/*$JR_EXPR_ID=39$*/

<------------------------>

 

I have the same problem on a subreport and I don't manage to solve it.

Could you help me please ?

 

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...