4

I have a list of goals, each of which have another ArrayList in it. I want to have the details in the child ArrayList to be displayed using a subreport. I would like to have a subreport for each of the objects in the child ArrayList.

The issue I am facing is that, I can't seem to find a way to specify the ArrayListas data source for the subreport. When trying to create datasource, I can't select fields of the dataset on which the list is built, only the fields of the main report can be selected.

Is it possible to do this in jasper report? I am stuck with this for quite some time now.

Petter Friberg
  • 21,252
  • 9
  • 60
  • 109
Victo
  • 61
  • 1
  • 5
  • Can you add some code that you tried?... like this the question is very "fussy", I can try to answer but I need to guess what you really like to do... How is your bean object (some code), what do you like to pass to the subreport?...ecc. – Petter Friberg Nov 02 '15 at 11:51
  • Possible duplicate of [How to pass main report data source to subreport (JasperReports)?](http://stackoverflow.com/questions/8490563/how-to-pass-main-report-data-source-to-subreport-jasperreports) – Alex K Oct 22 '16 at 19:14

1 Answers1

4

With information given this would be the answer.

Main bean (your goals?), containing List of other bean (SubBean).

public class Bean {
  private String var1;
  private List<SubBean> subBeans;
  public String getVar1() {
    return var1;
  }
  public void setVar1(String var1) {
    this.var1 = var1;
  }
  public List<SubBean> getSubBeans() {
    return subBeans;
  }
  public void setSubBeans(List<SubBean> subBean) {
    this.subBeans = subBean;
  }
}

Sub bean

public class SubBean {
  private String var2;
  public SubBean(String var2){
    this.var2 = var2;
  }
  public String getVar2() {
    return var2;
  }
  public void setVar2(String var2) {
    this.var2 = var2;
  }
}

How to pass the SubBean List to a subreport.

  1. You need the field subBeans in you main report.

    <field name="subBeans" class="java.util.List"/>
    
  2. I suggest that you pass the location of the .jasper file as a parameter (Note jasper report needs absolute path) es. in main report

    <parameter name="SUBREPORT_DIR" class="java.lang.String" isForPrompting="false"/>
    

and in java pass it (in example sub folder "jasper" in working directory is the location of the subreport.jasper)

paramMap.put("SUBREPORT_DIR", new File("jasper").getAbsolutePath() + File.separator);
  1. Now just call your subreport (needs to be complied into .jasper) from main report like this.

    <subreport>
      <reportElement x="105" y="4" width="400" height="100"/>
      <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{subBeans})]]></dataSourceExpression>
      <subreportExpression class="java.lang.String"><![CDATA[$P{SUBREPORT_DIR} + "subreport.jasper"]]></subreportExpression>
    </subreport>
    

Hence:

I'm creating a new JRDataSource for the sub report passing the List of SubBean in the current Bean

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

indicating the absolute location of the compiled subreport.jasper

$P{SUBREPORT_DIR} + "subreport.jasper"

so now in your subreport you can use the field: var2 of the SubBean, just define it like this in the subreport.jrxml

<field name="var2" class="java.lang.String"/>
Petter Friberg
  • 21,252
  • 9
  • 60
  • 109
  • Thanks for the reply. Essentially I have a parent list component in ireport. Inside that I have the subreport. The subBeans field is part of the dataset of the list component and not the main report. I understand its possible to use main report fields to develop datasource expressions, my problem is I want to use a field from my dataset. Is that possible? – Victo Nov 02 '15 at 12:47
  • "I want to use a field from my dataset", in the main report??, .. or the main Bean in the subreport?. Try to edit question with your Beans and some jrxml – Petter Friberg Nov 02 '15 at 12:50