Optional Grouping [message #1840829] |
Mon, 26 April 2021 09:38 |
Bob D Messages: 2 Registered: November 2017 |
Junior Member |
|
|
I'm trying to create a report where the grouping in the report is optional. If a checkbox is ticked, then a grouping should be applied on the table. And if the checkbox is unticked, then there should be no grouping applied, and the data should be presented as a flat table.
Here are a couple of approaches that I've attempted:
1) Set a 'Group On' expression which is based on the report parameter. When the report parameter is true, then this Group On expression will return the field to group on, e.g.
if(params["groupByCountry"].value){
row["COUNTRY"];
}
This solution kinda works, but when I export to Excel there are a few niggly issues.
Firstly, the group header is displayed even when the grouping is disabled. A workaround for this issue is to also make the display of the group header to be conditional on the group parameter.
Secondly, the excel document has an outline which has a collapsable grouping even when the checkbox is unticked. Ideally it would be good to not display this outline when grouping is disabled.
Please see the attached file (ParameterizedGrouping1.rptdesign) for an example of this approach.
2) Use javascript to manually create the group
The code looks something like the following:
importPackage(Packages.org.eclipse.birt.report.model.api);
importPackage(Packages.java.io);
if (params["groupByCountry"].value) {
var reportDesignHandle = reportContext.getDesignHandle();
var table = reportDesignHandle.findElement("Table");
var elementFactory = reportDesignHandle.getElementFactory();
var group = elementFactory.newTableGroup();
group.setName("COUNTRY");
group.setKeyExpr( "row[\"COUNTRY\"]" );
group.setSortDirection("asc");
var row = elementFactory.newTableRow(1);
var cellHandle = row.getCells().get(0);
var data = elementFactory.newDataItem(null);
data.setResultSetColumn("COUNTRY");
cellHandle.getContent().add(data);
group.getHeader().add(row);
table.getGroups().add(group, 1);
}
An example of this is provided in the attached file (ParameterizedGrouping2.rptdesign).
The benefit of the first approach is that it's simple and can be done in the report designer, without much custom code. However, it has a few niggly issues.
The benefit of the second approach is that it overcomes some of the niggly issues, but the JS code is more cumbersome to write.
Are there any other approaches which could be used other than those described above?
|
|
|
|
Powered by
FUDForum. Page generated in 0.03986 seconds