user1486984
user1486984

Reputation: 297

JasperReports: Passing in a list of lists as a datasource

I need to populate a few subreports with lists of different objects. Basically lets say i have the following:
Subreport on used Vehicles
Subreport on new Vehicles

I create a vehicle bean class with variables as strings and create getter and setter methods for the same. Then in my datasource I pass in a List<List<String>> as detailRows. detailRows contains a list for new vehicles and a list for used vehicles. So lets say, i pass detailRows in the data source.
Question is how do i pass these two lists to the two sub-reports? Can i use new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{newVehiclesList}) as a datasource for sub report 1 and new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{usedVehiclesList}) as datasource for sub report 2?
Is there anything else that needs to be done apart from what i mentioned? Do i need to create and pass any variables? Is the appropriate use of the list of lists as i have listed above or is it $F{detailRows}.get(0)?

I created a field detailRows in the main report as type list. I then pass the following to the subreport data source expression, new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{detailRows} Is there any way i can pass the newVehiclesList from detailRows to the sub-report?

Thanks!

Upvotes: 4

Views: 18902

Answers (3)

Maicon Gobbi
Maicon Gobbi

Reputation: 1

i was the same problems with you and i solved it using the tag List of jasper, i used datasource in my class java, for example: parameter.put("MyList", new JRBeanCollectionDataSource(ListObjects)); in JRXML

In palete of Jasper, choose the tag LIST and drag and drop in your relatory after choose

  • create new dataset
  • create new dataset from a connection… ...
  • in data adapter choose new data adapter - collection of javabeans
  • use a JRDatasource expression
  • go in lis of parameters and choose you list op objects (MyList)

now go to outline of jasper and - dataset properties - edit and query filter ... ... - javabean - search you class (I using eclipse, so it's easy to search my class) - add fields to use

Upvotes: 0

Diego Urenia
Diego Urenia

Reputation: 1630

Selecting your SubReport you can set the property "Connection type" as "Use a data source expression" and inside the property "Data Source Expression" you set this:

new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{yourFieldHere})

Where your "yourFieldHere" is a list (don't forget to set the "Field Class" inside your field properties as a java.util.List as well)

Upvotes: 6

Diego Urenia
Diego Urenia

Reputation: 1630

Ok, then you need create two fields with the Field Class as java.util.List, one for each list (newVehiclesList and usedVehiclesList).

Put your two SubReports wherever you want and click on each one doing the following steps:

Change the "Connection type" to "Use a datasource expression" then change the "Data Source Expression" to new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{yourField})

Done.

ps: In order to use the fields inside your newVehiclesList and usedVehiclesList you have to create them inside of their own subReports.

Upvotes: 1

Related Questions