JasperReports: Repeating subreport for each value of the list parameter

jasper-reportssubreport

I need to develop a report in JasperReports which contains ONE sub-report but the sub-report is repeated for every value in the list parameter.for example :

Parameter List to be passed to ONE subreport : 1,2,3,4

MyReport :

  • Subreport(Crosstable+ page break) for value 1 of the passed parameter
  • Subreport(Crosstable+ page break) for value 2 of the passed parameter
  • Subreport(Crosstable+ page break) for value 3 of the passed parameter
  • Subreport(Crosstable+ page break) for value 4 of the passed parameter**

Then the Subreport will also contain another "sub-Subreport" that should be repated X time for each value, example :
Subreport(Crosstable+ page break) for value 1 of the passed parameter :

  • "sub-subreport"(another Crosstable) for value 1.1
  • "sub-subreport"(another Crosstable) for value 1.2
  • "sub-subreport"(another Crosstable) for value 1.3

In other words i'm trying to loop on the same subreport but with different data something like :

for(i=0; i<list.lenght();i++){
   print(subreport(i));
}

I don't to know how it can be done. I hope my examples are clear. Thanks in advance

Best Answer

Your detail band will fire for each record. So, if your input control is $P{myCustomers} and your main report's query is:

SELECT customer_id
FROM customers
WHERE $X{IN, customer_id, myCustomers}

Then your main report will have one field, $F{customer_id}. Your subreport needs to have a parameter of its own and it will get a value from the main report. Insert your subreport into the detail band of the main report. Click on the subreport from within the main report, and then in the Properties window of the main report, click on Parameters. The "Name" column is the name of the parameter in the subreport that will accept the incoming value, and the "Expression" field is the value to pass. So if your subreport also has a parameter for customer id, the name is customer_id and the expression to pass is $F{customer_id}.

The iReport documentation (http://community.jaspersoft.com/documentation/ireport-ultimate-guide) has information about subreports on page 90.

Related Topic