views:

5407

answers:

8

Hi,

I have a Jasper report template, and I need to show/hide a column at runtime based on a particular condition. I'm using "Print when expression" to conditionally show/hide this column (and it's header) in my report. When the column is hidden, the space it would have occupied is left blank, which is not particularly attractive.

I would prefer if the extra space was used in a more effective manner, possibilities include:

  • the width of the report is reduced by the width of the hidden column
  • the extra space is distributed among the remaining columns

In theory, I could achieve (1) by setting the width of the column (and header) to 0, but also indicate that the column should resize to fit it's contents. But AFAIK Jasper does not actually provide a "resize width to fit contents" option.

Another option is to generate my reports using the Jasper API instead of defining the report template in XML. But that seems like a lot of effort for such a simple requirement.

Thanks in advance, Don

A: 

If it is just one column, is it possible to place this column to the far right, and then use the print when expression. That way there is not a hole in the middle. I know this is not ideal, as I had tried to do what you are currently trying to accomplish in the past, and could not find what I call a good solution.

A second idea would to be create a second report based on the first with out the column, and then when calling the report check the condition, to decide which one to call. Again not ideal, but would work.

I know this is not really the answer you were looking for, but one of these suggestions may work for you.

jschoen
The first solution won't really work because there is more than one column and there is a background color on the report header, so it will still look odd if there are missing columns. The second solution involves duplicating the report which is the very issue I'm trying to avoid :) Thanks anyway!
Don
+1  A: 

A slight variation on the "second report" theme that I have used is to isolate the part of the report where you have an optional column into it's own subreport, and then create two subreports, one with and one without the column, and then use conditions to determine which subreport to print.

Doug
A: 

I'm actually running into the opposite problem. I am using the printWhenExpression option and providing a boolean value. The values are shown/hidden correctly, but all the columns are shifted to fill in the empty spot - this is what I DON'T want, I would like to show the blank spaces where the values would be. I'm using iReport to create my reports.

Has anyone encountered this? I'm not sure why I'm getting the opposite effect. Any suggestions? (I don't want to simply fill the values in with blank strings because I have a sorting option in my reports and this would result in the wrong sort order) I would like the actual values to be stored, just hidden.

As you`re describing a completely different problem, you should probably post this as a separate question
Don
A: 

even i m facing the same problem of hiding columns during runtime.?Don didi u got any solution so far? Anna can u tell me how can u sort columns in ireport

A: 

Check THIS In that tutorial they are using XML template with Velocity framework. This is pretty complex. And to make it simpler you can us DynamicJasper. This library is an open source Java API that works over JasperReports that solves the dynamic columns issue.

Maksim
A: 

hi ,i hv some problem in jasper report.

suppose i hv colum address..

                       address

                     Abd,XYS,qwew

i want to show the above value like below pattern

                       address

                        Abd,
                        XYS,
                       qwew

how to show this file.i hv one jrxml file wr i design the report design and one java file wr fill the report. if any body hv any idea pls send me mail at [email protected]

puneet
A: 

Remove line when blank: This option takes away the vertical space occupied by an object, if it is not visible; the element visibility is determined by the value of the expression contained in the Print when expression attribute. Think of the page as a grid where the elements are placed, with a line being the space the element occupies. Figure 4-17 highlights the element A line; in order to really remove this line, all the elements that share a portion of the line have to be null (that is, they will not be printed).

Rudi
A: 

I recommend to use DynamicReports, it's open source and based on JasperReports. The main benefit of this library is a dynamic report design and no need for a visual report designer.

Ricardo