2004 IR Help Posted August 20, 2006 Share Posted August 20, 2006 By: Brad Balmer - bbalmer Passing data to subreport 2005-03-18 10:36 I am having a very hard time getting my subreport to display and there are no examples out there that I've seen that show how to do this passing a datasource for both the master and detail sections. Here is how I want my report to look: Hdr Data 1 Sub Data 1 Sub Data 1 Hdr Data 2 Sub Data 2 Etc... Both the header and sub data I wrote classes implementing JRDataSource and pass the constructor an ArrayList of beans. I have the header displaying but now cannot get the sub data to display. How I am linking JRDataSource dataSource = new RteSumHdrDataSource(lRteSumRptHdrList); JasperReport subreport = (JasperReport)JRLoader.loadObject(subReportLocation); Map parameters = new HashMap(); parameters.put("RTE_SUM_SUB_DATA", lRteSumDtlMap); parameters.put("RTE_SUM_SUB_RPT", subreport); File jasperReport = new File(reportLocation); reportData = JasperRunManager.runReportToPdf( jasperReport.getPath(), parameters, dataSource); My lRteSumDtlMap for the subreport is a HashMap of JRDataSource Objects where the key in the HashMap is the parameter connecting the Header to the Sub Data. Is this correct? In my report I have: <parameter name="RTE_SUM_SUB_DATA" class="java.util.HashMap"> </parameter> <parameter name="RTE_SUM_SUB_RPT" class="net.sf.jasperreports.engine.JasperReport"> </parameter> And my subreport: <subreport> <reportElement positionType="Float" x="6" y="144" width="739" height="229"/> <subreportParameter name="resourceid"> <subreportParameterExpression> <![CDATA[$F{resourceid}]]> </subreportParameterExpression> </subreportParameter> <dataSourceExpression> <![CDATA[$P{RTE_SUM_SUB_DATA}]]> </dataSourceExpression> <subreportExpression class="net.sf.jasperreports.engine.JasperReport"> <![CDATA[$P{RTE_SUM_SUB_RPT}]]> </subreportExpression> </subreport> Am I even doing this correctly? How do I pass data for a subreport and then for each main part of the report grab data to display for the subreport? Thanks for ANY help anybody can give. By: C-Box - c-box RE: Passing data to subreport 2005-03-18 13:21 Well, AFAIK, you can't link a customDataSource easily to a current masterrecord. With a connection and queries it's no problem - but not so with CustomDataSources.. ThereFore I wrote a so called "FilterableDataSource" that returns in it's next-method just the data from the SubDataSource that are linked over a FilterField and a FilterValue. So I do use as DataSourceExpression something like this: $P{DataContainer}.getFilterableDataSource("SubData1","ParentID",$F{ID}) So I just pass a so called DataContainer to the MasterReport when filling. That container holds all the needed SubDataSources and also all needed SubReports as Objects within some lists. (lmust create and put all the needed data to that DataContainer, so you have just one parameter and not too many if you are using multiple SubReports) With the public method getFilterableDataSource(String subDataSourceName,String filterFieldName,Object filterValue) I can return the needed CustomDataSource that just returns the matching records from the SubDataSource that fullfills the filterField equals with the filterValue when the JasperReportsEnginge iterates through the nextMethod. I posted that class some months ago somewhere here in the forum (openDiscussion when I remember right). Use the search function for further details. hth C-Box BTW: you should try to define your Parameter not as Map but as JRDataSource instead. So at least ALL SubRecords should appear. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now