Jump to content
Changes to the Jaspersoft community edition download ×

subreports using java bean data source


wexwarez

Recommended Posts

I have purchased the manuals and everything but there is no example of creating subreports with a java bean data source. I can't figure out how to do it. For instance if you were creating an inventory report. You might have a report of product categories with a subreport for each item. I would have a java bean Category that contains a property that is a Collection of items. The category report would loop on the category bean with a subreport for the items collection.

 

How do you set the subreports data source from a field registered from the category java bean data source?

 

thanks

Link to comment
Share on other sites

  • Replies 4
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

If you have a List getItems() method that returns item beans, all you need to do is to map a field to the items collection and use create a JRBeanCollectionDataSource when instantiating the subreport:

Code:

<import value="net.sf.jasperreports.engine.data.JRBeanCollectionDataSource"/>
..
<field name="items" class="java.util.List"/>
..
<subreport>
..
<dataSourceExpression>
new JRBeanCollectionDataSource($F{items})
</dataSourceExpression>
..
</subreport>

 

HTH,

Lucian

Link to comment
Share on other sites

Example of master report bean:

Code:

private String name;
private String ID;
private JRDataSource workListDS;

 

Example of sub report bean:

Code:
[code]
private String ID;
private String workName;

 

STEP_1:

Use an ArrayList to hold all sub report bean(s)

Code:
[code]
arrayList.add(subBean1);
arrayList.add(subBean2);
...

 

STEP_2:

Put list into data source, and set on master bean

Code:
[code]
JRDataSource jrds=new JRBeanCollectionDataSource(arrayList);

masterBean.setWorkListDS(jrds);

 

STEP_3:

Use DataSource Expression for SubReport in iReport

Code:
[code]
$F{workListDS}

 

Hope it helps~:)

 

--

We are coming from TAIWAN ~_~

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...