java - How to show/hide a column at runtime? -


i'd show/hide column @ runtime based on particular condition. i'm using "print when expression" conditionally show/hide column (and it's header) in report. when column hidden, space have occupied left blank, not particularly attractive.

i prefer if space used in more effective manner, possibilities include:

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

in theory, achieve first setting width of column (and header) 0, indicate column should resize fit contents. jasperreports not provide "resize width fit contents" option.

another possibility generate reports using jasper api instead of defining report template in xml. seems lot of effort such simple requirement.

in later version (v5 or above) of jasper reports can use jr:table component , truly achieve (without use of java code using dynamic-jasper or dynamic-reports).

the method using <printwhenexpression/> under <jr:column/>

example

sample data

+----------------+--------+ |      user      |  rep   | +----------------+--------+ | jon skeet      | 854503 | | darin dimitrov | 652133 | | balusc         | 639753 | | hans passant   | 616871 | | me             |   6487 | +----------------+--------+ 

sample jrxml

note: parameter $p{displayrecordnumber} , <printwhenexpression> under first jr:column

<?xml version="1.0" encoding="utf-8"?> <jasperreport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="reputation" pagewidth="595" pageheight="842" columnwidth="555" leftmargin="20" rightmargin="20" topmargin="20" bottommargin="20" uuid="a88bd694-4f90-41fc-84d0-002b90b2d73e">     <style name="table">         <box>             <pen linewidth="1.0" linecolor="#000000"/>         </box>     </style>     <style name="table_th" mode="opaque" backcolor="#f0f8ff">         <box>             <pen linewidth="0.5" linecolor="#000000"/>         </box>     </style>     <style name="table_ch" mode="opaque" backcolor="#bfe1ff">         <box>             <pen linewidth="0.5" linecolor="#000000"/>         </box>     </style>     <style name="table_td" mode="opaque" backcolor="#ffffff">         <box>             <pen linewidth="0.5" linecolor="#000000"/>         </box>     </style>     <subdataset name="tabledataset" uuid="7a53770f-0350-4a73-bfc1-48a5f6386594">         <field name="user" class="java.lang.string"/>         <field name="rep" class="java.math.bigdecimal"/>     </subdataset>     <parameter name="displayrecordnumber" class="java.lang.boolean">         <defaultvalueexpression><![cdata[true]]></defaultvalueexpression>     </parameter>     <querystring>         <![cdata[]]>     </querystring>     <title>         <band height="50">             <componentelement>                 <reportelement key="table" style="table" x="0" y="0" width="555" height="47" uuid="76ab08c6-e757-4785-a43d-b65ad4ab1dd5"/>                 <jr:table xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemalocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd">                     <datasetrun subdataset="tabledataset" uuid="07e5f1c2-af7f-4373-b653-c127c47c9fa4">                         <datasourceexpression><![cdata[$p{report_data_source}]]></datasourceexpression>                     </datasetrun>                     <jr:column width="90" uuid="918270fe-25c8-4a9b-a872-91299cddbc31">                         <printwhenexpression><![cdata[$p{displayrecordnumber}]]></printwhenexpression>                         <jr:columnheader style="table_ch" height="30" rowspan="1">                             <statictext>                                 <reportelement x="0" y="0" width="90" height="30" uuid="5cd6da41-01d5-4f74-99c2-06784f891d1e"/>                                 <textelement textalignment="center" verticalalignment="middle"/>                                 <text><![cdata[record number]]></text>                             </statictext>                         </jr:columnheader>                         <jr:detailcell style="table_td" height="30" rowspan="1">                             <textfield>                                 <reportelement x="0" y="0" width="90" height="30" uuid="5fe48359-0e7e-44b2-93ac-f55404189832"/>                                 <textelement textalignment="center" verticalalignment="middle"/>                                 <textfieldexpression><![cdata[$v{report_count}]]></textfieldexpression>                             </textfield>                         </jr:detailcell>                     </jr:column>                     <jr:column width="90" uuid="7979d8a2-4e3c-42a7-9ff9-86f8e0b164bc">                         <jr:columnheader style="table_ch" height="30" rowspan="1">                             <statictext>                                 <reportelement x="0" y="0" width="90" height="30" uuid="61d5f1b6-7677-4511-a10c-1fb8a56a4b2a"/>                                 <textelement textalignment="center" verticalalignment="middle"/>                                 <text><![cdata[username]]></text>                             </statictext>                         </jr:columnheader>                         <jr:detailcell style="table_td" height="30" rowspan="1">                             <textfield>                                 <reportelement x="0" y="0" width="90" height="30" uuid="a3cdb99d-3bf6-4c66-b50c-259b9aabfaef"/>                                 <box leftpadding="3" rightpadding="3"/>                                 <textelement verticalalignment="middle"/>                                 <textfieldexpression><![cdata[$f{user}]]></textfieldexpression>                             </textfield>                         </jr:detailcell>                     </jr:column>                     <jr:column width="90" uuid="625e4e5e-5057-4eab-b4a9-c5b22844d25c">                         <jr:columnheader style="table_ch" height="30" rowspan="1">                             <statictext>                                 <reportelement x="0" y="0" width="90" height="30" uuid="e1c07cb8-a44c-4a8d-8566-5c86d6671282"/>                                 <textelement textalignment="center" verticalalignment="middle"/>                                 <text><![cdata[reputation]]></text>                             </statictext>                         </jr:columnheader>                         <jr:detailcell style="table_td" height="30" rowspan="1">                             <textfield pattern="#,##0">                                 <reportelement x="0" y="0" width="90" height="30" uuid="6be2d79f-be82-4c7b-afd9-0039fb8b3189"/>                                 <box leftpadding="3" rightpadding="3"/>                                 <textelement textalignment="right" verticalalignment="middle"/>                                 <textfieldexpression><![cdata[$f{rep}]]></textfieldexpression>                             </textfield>                         </jr:detailcell>                     </jr:column>                 </jr:table>             </componentelement>         </band>     </title> </jasperreport> 

output $p{displayrecordnumber}=true

true

output $p{displayrecordnumber}=false

false

as can see columns adapts nicely on basis of displayed.


Comments

Popular posts from this blog

c++ - How do I get a multi line tooltip in MFC -

asp.net - In javascript how to find the height and width -

c# - DataTable to EnumerableRowCollection -