4

In Jasper Reports, is it possible to dynamically change the width or position of a column based on certain criteria?

For example, we have a report that contains the following columns:

ID | Name | Course | Status |

Now, there is an option to allow the report to only display a person's ID, as opposed to the ID and Name. If we make the name column null, we end up with a huge empty gap between the ID and Course columns:

ID |_____| Course | Status

Is it possible to programmatically reposition the remaining columns, so that they all shift up to the be next to the ID column?

sim
  • 3,552
  • 5
  • 23
  • 23
  • How do you pass the design into jasper? Is it doable to have two designs depending on the option selected? – Guillaume Jan 19 '11 at 19:08
  • It is possible to have 2 designs, but as yshalbar said below, if new options are added in the future, we would need to create a whole new report design for each one. That's not the most maintenance-friendly solution. – sim Jan 21 '11 at 06:04
  • See the following link It might help :) http://stackoverflow.com/questions/27181665/how-make-an-table-with-dynamic-columns-in-jasper-studio/27182733#27182733 – Ana Mar 06 '15 at 10:52

1 Answers1

2

We had the same problem, and didn't find a solution, just ugly workarounds.

We started with a simple solution of having two designs, when we had two options.

When more options arrived, this became impossible to maintain. We now use a dynamic structure:

$P{field_name1} | $P{field_name2} 
------
$F{value1} | $P{value2} ...

We pass the field names as parameters, and use a dynamic data-source. We don't show the last, empty columns if exist. Note that we needed to dynamic query and populate the data-source in code, so this may not be a viable solution.

Another option you have is you create the Jasper Design in code (We also do this, in another scenario) - we load a jrxml template, and then dynamically add bands and items as needed.

JasperDesign jasperDesign = JRXmlLoader.load(myJrxml);
yshalbar
  • 1,455
  • 1
  • 9
  • 23