Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access the root element of the datasource in jasperreports

I have a report backed by a collection of MyJavaBean.

In this report I (of course) can get the properties of MyJavaBean declaring them in the Fields and using it on the details band, so far so good.

Now I want to be able to pass this MyJavaBean as a parameter of a subreport. Look that I want to be able to pass the javabean itself, not one of its propertys.

How can I make reference to one element of my collection in the detais band?

like image 738
Lucas Machado Avatar asked Jul 29 '11 14:07

Lucas Machado


1 Answers

Referencing a bean

To declare a field that references the bean itself instead of one of its properties, set the field description to the keyword _THIS.

<field name="myJavaBean" class="com.your.package.MyJavaBean">
    <fieldDescription>_THIS</fieldDescription>
</field>

You can then pass this as a subreport parameter like any other field.

<subreportParameter name="myJavaBean">
    <subreportParameterExpression>
        <![CDATA[$F{myJavaBean}]]>
    </subreportParameterExpression>
</subreportParameter>

Methods in the bean can be called in the normal way, i.e: $F{myJavaBean}.someMethod()

Referencing a single element of the collection

Depending on what you are doing here it could be more difficult. If you want to only see the detail for the single element, set the printWhenExpression on the band to the key of the element you want. However, if you want to have some report elements reference one object in the collection while the rest of the band references another, it would probably be better for you to nest another subreport within the detail band.

like image 131
GenericJon Avatar answered Nov 19 '22 03:11

GenericJon