Search code examples
javajasper-reports

Jasper report displays group header when first two/more elements' headers are equal


A query is executed from within a facade to generate the report data. The query is correctly grouping data as follows: one row with A=351 (the one with B=757), rows with A=352 (B=523/535/etc.), and so on.

If the first row appearing in the page has A=350, then the second has a value diferente from 350 (e.g. 351), the "351" header will be displayed in the next page (only if there are enough 351 rows to reach another page, of course).

enter image description here

The 2nd and 3rd rows should not be inside 351 group, but inside another group called 352. But this behavior is not always happening for other pages. In this case, both descriptions are equal, but still 352 is being displayed on page 11.

Is it a bug in JasperReports?

I used to do a programmatic sort instead of SQL GROUP BY, as follows, and it didn't change anything:

Collections.sort(Reports.LIST,

    (Comparator<MyObject>) new Comparator<MyObject>() {

        public int compare(MyObject o1, MyObject o2) {

            if (o1.getA().compareTo(o2.getA()) == 0) {
                return o1.getB().compareTo(o2.getB());
            } else {
                return o1.getA().compareTo(o2.getA());
            }

        }

    }

);

EDIT: Found out it happens in this specific case wherever the row is located (the 352 one is the last one in this page):

enter image description here


Solution

  • The Group Expression option has only the description, not the number; so, when the descriptions were equal for same number, it didn't separate the rows.

    Steps:

    1. Open iReport and your file;
    2. Go to the Report Inspector (the left pane)
    3. Click on the desired Group Header and open the Properties pane (right side)
    4. Under the last section Group properties, type the correct Group Expression.