简体   繁体   English

Jasper Reports:Subeport 在主报告中显示空值,数据源是一个 java 列表

[英]Jasper Reports: Subeport showing null values in Main Report, datasource is a java List

WHAT I HAVE我拥有的

My Main Report shows all values are null from my subreport.我的主报告显示我的子报告中的所有值都为空。

在此处输入图片说明

WHAT I WANT我想要的是在此处输入图片说明

I am passing data to subReport from MainReport through a parameter called subData as shown in the MainReport JRXML snippet below.我正在通过名为 subData 的参数从 MainReport 将数据传递到 subReport,如下面的 MainReport JRXML 片段所示。

NB : Its a List注意:它是一个列表

在此处输入图片说明

My subReport snippet in the MainReport is given below, the dataSource expression is clearly "$P{subData}"下面给出了我在 MainReport 中的 subReport 片段,数据源表达式显然是“$P{subData}”

在此处输入图片说明

My main java class passes an instance of a List (named subData) through a parameter (named subDataOrion) shown below.我的主要 java 类通过如下所示的参数(名为 subDataOrion)传递一个 List(名为 subData)的实例。

public class SubRepoExample {

public static void main(String[] args) throws JRException {
   SubRepoExample repo = new SubRepoExample();
   repo.combineMasterAndSubreport();
}

public void combineMasterAndSubreport() throws JRException{

    JasperReport main = JasperCompileManager.compileReport("src/subrepoexample/masterReport.jrxml");

    JasperReport sub = JasperCompileManager.compileReport("src/subrepoexample/subReport.jrxml");



   //create a list for holding the subreport object

    //SubreportWrapper subDataWrap = new SubreportWrapper();

    List<SubreportObject> subData = new ArrayList();
    //subData.add(subDataWrap.getSubData());
    subData.add(new SubreportObject("Kevin",20));
    subData.add(new SubreportObject("Jane",20));
    subData.add(new SubreportObject("Mike",20));
    subData.add(new SubreportObject("Simon",20)); 
    //subData.add(new SubreportObject("Naomi",25));
    //subData.add(new SubreportObject("Pat",20));


    //SubreportWrapper subDataWrap = new SubreportWrapper();

    //List<SubreportObject> subData = subDataWrap.getSubData();

    Map para = new HashMap();
    //pass the report itself through a parameter
    //para.put("SUBREPORT", sub);
    //pass the list to parameter
    //JRDataSource subx = new JRBeanCollectionDataSource(subData);
    para.put("subDataOrion",subData);

    //JasperPrint jp = JasperFillManager.fillReport(sub, para,new JRBeanCollectionDataSource(subData));      
    //JasperPrint jp = JasperFillManager.fillReport(main, para,new JRTableModelDataSource(createMasterData()));


     JasperCompileManager.compileReportToFile(
            "src/subrepoexample/subReport.jrxml", 
            "src/subrepoexample/subReport.jasper"); 


    JasperPrint jp = JasperFillManager.fillReport(main, para,new JRTableModelDataSource(createMasterData()));
    JasperViewer.viewReport(jp, false);  

}

 public DefaultTableModel createMasterData(){

    String [] cols = {"COL_1","COL_2","COL_3"};
    Object[][] data = {{"Data","Data","Data"},
                       {"Data","Data","Data"},       
                       {"Data","Data","Data"},   
                       {"Data","Data","Data"},   
                       {"Data","Data","Data"},  
                      };
    DefaultTableModel dtm = new DefaultTableModel(data,cols);

return dtm;}

}

SubReport class that creates the subReport objects创建子报表对象的子报表类

public class SubreportObject {
  String name;
  int age;

 public SubreportObject(String name,int age){
     this.name = name;
     this.age = age;  
     }

 public String getName() {
 return name;
     }

 public void setName(String name) {
 this.name = name;
     }

 public int getAge() {
 return age;
     }

 public void setAge(int age) {
 this.age = age;
     }

  }

I am aware there are two issues here, the arrangement issue of the data since the subreport data is mixed.我知道这里有两个问题,数据的排列问题,因为子报表数据是混合的。 or scrambled with the main report data as shown in the first screen shot.或与第一个屏幕截图中所示的主要报告数据进行混杂。 This I think can be fixed by groups, which is not my question here.我认为这可以由小组解决,这不是我在这里的问题。

MY QUESTION :我的问题

I need first to eliminate the 'null' values so that I can go to the next stage of using group expression.我首先需要消除“空”值,以便我可以进入使用组表达式的下一阶段。 Am stuck here and not sure what is wrong?被困在这里,不知道出了什么问题? At least I asked this question and at least 'null' values appeared, earlier they were completely not appearing.至少我问了这个问题并且至少出现了“空”值,之前它们完全没有出现。

Why is the data in the List appearing as 'null', when compiled separately, they compile wonderfully.为什么List中的数据显示为'null',单独编译时,它们编译得很好。

EDIT 1:编辑 1:

Sorry for being sloppy on the earlier version of this question.很抱歉在这个问题的早期版本上草率。 Thank you people.谢谢你们。

EDIT 2:编辑2:

I have added the JRBeanCollection as shown below我添加了 JRBeanCollection,如下所示

新代码

Even changed the parameter class to JRDataSource as below甚至将参数类更改为 JRDataSource 如下

在此处输入图片说明

but still shows null values..???但仍然显示空值..???

您缺少JRBeanCollectionDatasource ,List 本身不是数据源,除非它实现了 JRDataSource 接口应该由 JRDataSource 包装/改编。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM