Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic column cell width

I have some Jasper reports which are displayed in HTML format. I would like the width of the columns in the HTML tables to automatically resize to fit the content of the widest cell (in that column), such that all the data is displayed.

Currently this does not happen because the HTML generated by Jasper specifies fixed widths for the <table> and some <td> elements, e.g.

<td style="width: 20px; height: 17px;">
  <span style="font-family: Arial; font-size: 11px;">foo-bar-baz@examp</span>
</td>

I can't simply remove all these width properties (using JavaScript), because (as shown in the HTML above) any data that would be hidden when using these widths is not even returned to the client-side

Cheers, Don

like image 983
Dónal Avatar asked Nov 18 '08 22:11

Dónal


2 Answers

While not perfect, you could flag the field to stretch with overflow. This would at least give you all the data. In your jrxml file it would be similar to:

<textField isStretchWithOverflow="true" hyperlinkType="None">
    <reportElement style="Report Sub-Title" x="0" y="84" width="802" height="20"/>
    <textElement/>
    <textFieldExpression class="java.lang.String">
        <![CDATA["For the period ...]]>
    </textFieldExpression>
</textField>

I'm afraid I don't know of any feature of Jasper reports that would allow the dynamic resizing of column widths - It is probably one of those things that makes sense for HTML but little sense for other output formats such as PDF.

like image 185
Jamie Love Avatar answered Sep 19 '22 13:09

Jamie Love


The developers made a conscious decision to not allow cell width to stretch. For columns to change their width based on the content, it must be done through programming. A helpful tool: DynamicJasper.

DynamicJasper creates reports dynamically, defining at runtime the columns, column width (auto width), groups, variables, fonts, charts, crosstabs, sub reports (that can also be dynamic), page size and everything else that you can define at design time.

The JasperReports forum and DynamicJasper HOW-TO both have examples of automatic (dynamic) column widths.

FastReportBuilder drb = new FastReportBuilder();
drb.addColumn("State", "state", String.class.getName(),20)
        .addColumn("Branch", "branch", String.class.getName(),30)
        .addColumn("Quantity", "quantity", Long.class.getName(),60,true)
        .addColumn("Amount", "amount", Float.class.getName(),70,true)
        .addBarcodeColumn("Bar-Code", "amount", Long.class.getName(), BarcodeTypes.USD3, true,
false,null, 100, true, ImageScaleMode.FILL, null)
        .addGroups(1)
        .setDetailHeight(30)
        .setTitle("November 2006 sales report")
        .setSubtitle("This report was generated at " + new Date())
        .setUseFullPageWidth(true);     

DynamicReport dr = drb.build();

Passing in true when calling addColumn indicates to use a fixed width. Leaving the parameter off or passing false will set the column width dynamically. You may also have to call setUseFullPageWidth( true ) for the auto widths to activate, but I'm not sure on this point.

like image 35
Dave Jarvis Avatar answered Sep 19 '22 13:09

Dave Jarvis