JasperReport: Purpose of the Details Band

2019-02-05 03:08发布

I figured this would be something that would be fairly-well documented as its a central theme to JasperReports, however I can't find an answer for this anywhere.

What is the purpose/function/intention of a details band? Is it supposed to just be the central or core part of a report?

From another question it was pointed out to me that there is a 1:1 relationship between a details band and a record/bean provided by the JRDatasource. This revelation brings to light a few tangential questions:

  • It is possible to add detail bands programmatically in Java; what happens if you specify more/less detail bands (programmatically) than there are records/beans returned by your JRDataSource?
  • What is the relationship between a details band, and say, a page inside an exported PDF document? Does 1 details bands translate to 1 page?
  • What happens if you pass the JasperFillManager a null data source? Is it possible to still have detail bands?

I don't like to ask multiple questions at a time, but these are so similarly-related I'd rather do it all at once than clutter SO with multiple nearly-identical questions. Thanks in advance.

5条回答
我欲成王,谁敢阻挡
2楼-- · 2019-02-05 03:47

The details band is indeed the band where each element of the data source is reported. The report engine automatically iterates over the data source and inserts data into the template of the detail band with respective element of the data source.

You may of course have several elements on a single page. According to the properties of the band (split allowed, height, etc.), the paging will be handled by Jasper Reports automatically, and it's the engine that will thus decide how many elements are printed on each page, when to go to the following page, etc.

The details band is not printed if you have nothing in the data source. The printing of the other bands depend on the parameters of the report.

查看更多
We Are One
3楼-- · 2019-02-05 03:47

As I remember it, you can see the details band as the "model" for a row/record in the report. All elements you put inside the detail band will be repeated for each record provided by the JRDatasource.

For example, your details band might have two text fields, one with the value ${companyName}, and one with the value value ${revenue}.

If you now pass your report three rows, then "companyName" and "revenue" will be evaluated for each of them, and you may get something like:

|Apple              |  $1000,000,000|
|Microsoft          |   $500,000,000|
|My amazing company |            $12|

I.e., the detail band contents has been repeated three times.

So: - As you see you probably only need one details band configured. Not sure if it's allowed in the JRXML to have multiple ones. - There is no relationship between the details band and a page. Pagination is handled separately. - Not sure, to be honest. My best guess is either an exception or you get no rows where the detail band is supposed to be rendered.

查看更多
仙女界的扛把子
4楼-- · 2019-02-05 03:54

You misunderstand what a detail band is.

The detail band of your report will be printed for each of the elements in your datasource. If you use a database datasource, the detail will be printed as many times as rows has your resultset. If you use a JRBeanCollection datasource, the detail band will be printed as many times as items has your collection.

To answer your questions:

  1. You can't specify more than one detailband, it's illegal. You can use subreports for that purpose.

  2. The report will output as many detail bands as it can per page. On the otherhand, you can make the detail band the same height your desired report output will be (minus header/footer/etc height). If you want to print the report in an A4 paper you can make your detail band 297 mm high (considering you have no other bands). Then each detail band will be printed in a separate page.

  3. If you pass an empty datasource, the report will be generated with no pages.
查看更多
Emotional °昔
5楼-- · 2019-02-05 04:04

The information you have is correct, you've just had a slight misunderstanding of what it means.

The 1:1 relationship is at fill-time; Once the report has been filled there will be exactly 1 detail band for every record in the dataset. When you are designing the report you only add the band once, but that one band is repeated over and over again when the report is filled.

You can add as many detail bands as you wish. Each of the bands have a 1:1 relationship with the dataset records, and all of the detail bands are filled before moving on to the next record (i.e. If you have three bands A, B, and C; Their order in the report will be ABCABC... not AAA...BBB...CCC...).

  • It is possible to add bands programmatically, but the important point to note is that you are adding bands to the report design, not to the completed report. So just as with a jrxml design, you add the band once and it gets repeated for each record. Check this example.

  • There isn't a relationship between report pages and detail bands. The report filler will try to put as much onto a page as possible while respecting the splitType of each band as well as other report properties.

  • Is passing a null datasource is allowed? In any case the 1:1 relationship is still valid: A dataset with 0 records produces 0 detail bands in the report.

查看更多
倾城 Initia
6楼-- · 2019-02-05 04:06

The purpose of the detail band is to provide you with a model where you place and configure report elements. I don't think the relationship between a bean and detail band is one to one, since you can place many bean property elements in one bean. So, i would say the detail band is tightly tied to a collection of report bean elements.

One difference though, is the fact that detail band's functionality is irrelevant to how many beans/records you provide through a JRDataSource. The detail band will iterate through all of them until the data source is "consumed".

Furthermore, i personally find it very useful that the detail band allow you to iterate through a collection of bean properties. So, placing several properties in a detail band will iterate through all of them, before proceeding forward.

If you pass a null as DataSource you wont get any data on the report, and only static text will show up.

查看更多
登录 后发表回答