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 开发者_如何学Ca 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.
Just a side note - when you want to include code in your question, use the code button above the composition window. We can't see your example. If you click on the question mark inside the little orange square at the top right corner of the composition window it will take you to a Markdown editing tutorial.
I'm a little unclear as to what you are trying to accomplish. I'm not sure from the collection if you want to include all the subreports in your report and need a way to iterate through their names in the directory, or if you want the selection of the subreport to be dynamic. I don't know of a way to include more than one subreport in a main report. If you want to dynamically select the report name, why not just make the subreport name a parameter that gets passed to your report from your code. You can do your logic to decide which report name you want based on whatever criteria you choose. If that's not what you're looking for, perhaps a little more detail in your question would help.
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.
精彩评论