List into other List jasper report

2019-07-30 22:32发布

问题:

I have this object structure, to use like Java Bean in mi jasper report,

public class Person{
   private String name;
   private String lastname;
   private List<Job> jobs;
}

public class Job{
   private String jobName;
   private String companyName;
   private List<Reponsability> responsabilities;
}

All class with corresponding setters and getters

When I create a list into jasper report I define the "job list" JRDataSource expression this way:

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

Now I want to create another list, "responsibilities list" on the "job list", the question is:

How can I define the data source to get the corresponding job to pass into the JRDataSource Expression?

I imagine something like this:

new JRBeanCollectionDataSource(${jobs}.get($index).getResponsabilities())

but I can't get it work this

回答1:

I found the solution to do this:

Into the list, I created a variable called jobInstance

    <field name="jobInstance" class="com.mypackage.example.Job">
        <fieldDescription><![CDATA[_THIS]]></fieldDescription>
    </field>

Then when I set the datasource for responsibilities list did this:

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

References: https://community.jaspersoft.com/questions/508346/getting-current-object