我有一个参数,其表达式类是List。在List中是另一个列表,所以数据看起来像这样。
Class Bean{
String property1
String property2
Long property3
List<BeanDetails> beanDetails
}
请注意我将在列表中传递Bean。 (列出豆类)
1)是否可以在iReport中处理和读取此类数据?
2)我们如何声明/定义它并在iReport中使用它?
报告应如下所示
Col1 Col2 Col3 Col4 Col5 Col6
1 bean.id bean.name beanDetail.det1 beanDetail.det2 beanDetail.det3
beanDetail.det1 beanDetail.det2 beanDetail.det3
beanDetail.det1 beanDetail.det2 beanDetail.det3
2 bean.id bean.name beanDetail.det1 beanDetail.det2 beanDetail.det3
beanDetail.det1 beanDetail.det2 beanDetail.det3
beanDetail.det1 beanDetail.det2 beanDetail.det3
答案 0 :(得分:2)
有多种解决方案可以满足您的需求。
创建数据源的 flat 结构,例如List
Map<String,Object>
,循环Bean
,BeanDetails
示例< / p>
List<Map<String,Object>> mapList = new ArrayList<Map<String,Object>>();
for(Bean bean : yourBeans){
Map<String,Object> map = new HashMap<String,Object>();
map.put("col1", bean.getProperty1());
map.put("col2", bean.getProperty2());
map.put("col3", bean.getProperty3());
boolean first = true;
if (bean.getBeanDetails()==null|| bean.getBeanDetails().size()==0){
mapList.add(map);
}else{
for (BeanDetails bd:bean.getBeanDetails()){
if (!first){
map = new HashMap<String,Object>();
map."col1", "");//or use printWhenExpression != null in jrxml
....
}else{
first = false;
}
map.put("col4",bd.getDet1());
....
mapList.add(map);
}
}
}
//This now becomes your datasource
JRMapArrayDataSource datasource = new JRMapArrayDataSource(mapList);
使用子报告包含跨越col4到col6的子报告,主报告中的设置字段Bean
<field name="_THIS" class="com.your.package.Bean"/>
并作为数据源传递给子报告
<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{_THIS}.getBeanDetails())]]></dataSourceExpression>
创建自己的 JRDataSource (我不会提交整个类,但只提供一些关于如何完成此操作的提示,创建一个实现JRDataSource
的新类)
JRDataSource myDatasource = new JRDataSource() {
//TODO: keep controll of you list of Beans, current Bean and current BeanDetails, using pointers.
@Override
public boolean next() throws JRException {
//TODO: Implement if there are still records, move to next Bean or BeanDetails
boolean existsRecords = false;
return existsRecords;
}
@Override
public Object getFieldValue(JRField field) throws JRException {
String name = field.getName();
//TODO: On the basis of your pointer, current Bean and current BeanDetails, return the value requested.
return null;
}
};
做出选择,玩得开心!