How to pass a List of JRBeanCollectionDataSource to a subreport

喜欢而已 提交于 2019-12-28 16:08:11

问题


I am generating a report from a JRBeanCollectionDataSource. This report is about a customer's order.

This is my code

public class Customer
{
    private String customerName;
    private String customerNo;
    private String customerAddress;
    private ArrayList<CustomerOrder> customerOrders;
    //Getters and Setters
}


private class CustomerOrder
{
    private String itemName;
    private BigDecimal amount;
    private int itemQuantity;
    //Getters and Setters
}

When a customer a report containing the customer details and a list of the customer orders need to be generated. Since JRBeanCollectionDataSource takes a collection, this is what i did.

Customer cust; //Customer Instance

ArrayList<Customer> custList = new ArrayList<Customer>();
custList.add(cust); 

JRBeanCollectionDataSource rptData = new JRBeanCollectionDataSource(custList);

How can i extract the CustomerOrder list in Customer and pass it as a subreport?


回答1:


You should be able to set the datasource expression for the subreport:

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

The resulting xml should have a subreport tag that resembles:

<subreport>
    <reportElement uuid="e9fc4a60-3844-41b7-a38c-768f06f09b44" x="0" y="57" width="555" height="68"/>
    <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{customerOrders})]]></dataSourceExpression>
    <subreportExpression><![CDATA[$P{SUBREPORT_DIR} + "report2_subreport1.jasper"]]></subreportExpression>
</subreport>

The only other thing you need to check is that the Language for the report properties is set to Java.



来源:https://stackoverflow.com/questions/13384698/how-to-pass-a-list-of-jrbeancollectiondatasource-to-a-subreport

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!