Search code examples
dynamicjasper-reportssubreport

JasperReports: How to dynamically change subreportExpression


What I am trying to do is seemingly simple.

I have a master report that includes a subreport element. The subreport is populated from a JRAbstractBeanDataSource, that has a collection of 0-n subreports in it.

In the master report, the subreport is found with:

<subreportExpression  class="java.lang.String"><![CDATA[$P{SUBREPORT_DIR} + $F{subReportFileName}]]></subreportExpression>

This works fine, SUBREPORT_DIR is passed in as a param to the datasource in the code.

What I need to do though, is select a different report name, based on the subreport properties, something like:

<subreportExpression class="java.lang.String"><![CDATA[$P{SUBREPORT_DIR} + "$F{subReportFileName}"]]></subreportExpression>

That is, allow each item in the collection of subreport datas on the datasource specify what the .jasper filename will be (that is, allow it so select which subreport to use).

Maybe there is a better way to think about/approach this.


Solution

  • I was trying to find a way to set the report name as the master report iterated over the date source items. Instead I just made the query completely dynamic, passed in as a param. That's what I really needed to change for each sub-report.