什么是jasper报告使用数据源的算法?

时间:2010-04-28 21:39:38

标签: java jasper-reports

我通过实现JRDataSource接口创建了自定义数据源。此界面如下所示:

public interface JRDataSource
{
 /**
  * Tries to position the cursor on the next element in the data source.
  * @return true if there is a next record, false otherwise
  * @throws JRException if any error occurs while trying to move 
  * to the next element
  */
 public boolean next() throws JRException;

 /**
  * Gets the field value for the current position.
  * @return an object containing the field value. The object type must 
  * be the field object type.
  */
 public Object getFieldValue(JRField jrField) throws JRException;

}

我的问题如下:jasper报告以何种方式调用此函数来获取.jrxml中的字段。

E.g:

  if( next() )){
     call getFieldValue for every field present in the page header 
     while( next() ){
       call getFieldValue for every field present in detail part
     }
     call getFieldValue for every field present the footer
   }

以前只是一个例子,实验上我实际上发现它实际上不是那样的。所以我的问题出现了。

谢谢!

1 个答案:

答案 0 :(得分:1)

我实际上认为算法是这样的(简化方式并使用Java语法):

while(dataSource.next()) {
   for (JRField field : reportFields)
       currentValues.put(field, dataSource.getFieldValue(field));
}

字段在jrxml文件中声明,与它们显示的位置无关。

但是,据我所知,默认情况下,每个记录仅生成(绘制或更新)详细信息部分。换句话说,如果您需要更新页面页眉或页脚中的信息,则必须做一些奇特的事情。

我相信有一个evaluationTime属性used in several elements可能会对此有所帮助,或者您可能需要查看subreport功能,具体取决于您有多少数据需要合作。

就我个人而言,我发现JasperReports sample projects很有帮助,尽管它需要付出一些努力才能从中获得更多。我还指出The Definitive Guide to JasperReports,其名字是谎言,但确实是一个体面的(以及完全缺乏的)参考手册。