print when expression to display Static text if there is atleast a not null field in a detail band.

0

Hi ,

I want to put a printWhenExpression on a static text based on a condition which says that print the label only if there is atleast a non null textField in a particular column in detail band.

Its like print the column header only if there is a non null field in that column.

 

I tried putting this expression ( $F{textfield} != null ) but it works only for first record, if first record has null and second record has not null value then the label will be hidden, where as we want it to be displayed.

Please reply if you need any more info.

 

Regards,

ckgupta

ckgupta's picture
18
Joined: May 25 2011 - 1:15am
Last seen: 7 years 10 months ago

7 Answers:

1
Yea, attach your jrxml or a slimmed down version. I'm having a hard time following what you're trying to do.
frankhassanabad's picture
Joined: Oct 25 2010 - 11:12am
Last seen: 4 years 11 months ago
1

PFA  jrxml .

I would like to give a brief explanation on it .

Consider there is a table Student which has 3 columns Name, Class, RollNo

Name   Classs  RollNo

-------     ----------   -----------

John      <null>    102

Mark          XI         103

Robert      XII         104

So when I want to display it in report I want to write a printWhenExpression for Class i.e., column header which will be displayed only if there is any non null value available , so in the previous case Class should be displayed

 

=================================================

Now, if we consider an example as written below

Name   Classs  RollNo

-------     ----------   -----------

John      <null>    102

Mark       <null>        103

Robert    <null>     104

Then Class should not even be displayed in report as none of the tuples has non null field in it.

 

 

ckgupta's picture
18
Joined: May 25 2011 - 1:15am
Last seen: 7 years 10 months ago
0
Code:
<?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 <a href="http://jasperreports.sourceforge.net/xsd/jasperreport.xsd"" target="_blank">http://jasperreports.sourceforge.net/xsd/jasperreport.xsd"</a> name="report1" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
 <property name="ireport.zoom" value="1.0"/>
 <property name="ireport.x" value="13"/>
 <property name="ireport.y" value="0"/>
 <field name="name" class="java.lang.String"/>
 <field name="class" class="java.lang.String"/>
 <field name="rollNo" class="java.lang.String"/>
 <background>
  <band splitType="Stretch"/>
 </background>
 <title>
  <band height="20" splitType="Stretch">
   <staticText>
    <reportElement x="0" y="0" width="62" height="20"/>
    <textElement textAlignment="Center">
     <font size="12" isBold="true"/>
    </textElement>
    <text><![CDATA[Name]]></text>
   </staticText>
   <staticText>
    <reportElement x="72" y="0" width="62" height="20">
       <printWhenExpression><![CDATA[$F{class} != null]]></printWhenExpression>
    </reportElement>
    <textElement textAlignment="Center">
     <font size="12" isBold="true"/>
    </textElement>
    <text><![CDATA[Class]]></text>
   </staticText>
   <staticText>
    <reportElement x="147" y="0" width="62" height="20"/>
    <textElement textAlignment="Center">
     <font size="12" isBold="true"/>
    </textElement>
    <text><![CDATA[Roll No]]></text>
   </staticText>
  </band>
 </title>
 <pageHeader>
  <band splitType="Stretch"/>
 </pageHeader>
 <columnHeader>
  <band splitType="Stretch"/>
 </columnHeader>
 <detail>
  <band height="20" splitType="Stretch">
   <textField>
    <reportElement x="0" y="0" width="62" height="20"/>
    <textElement textAlignment="Center">
     <font size="12"/>
    </textElement>
    <textFieldExpression class="java.lang.String"><![CDATA[$F{name}]]></textFieldExpression>
   </textField>
   <textField>
    <reportElement x="72" y="0" width="62" height="20"/>
    <textElement textAlignment="Center">
     <font size="12"/>
    </textElement>
    <textFieldExpression class="java.lang.String"><![CDATA[$F{class}]]></textFieldExpression>
   </textField>
   <textField>
    <reportElement x="147" y="0" width="62" height="20"/>
    <textElement textAlignment="Center">
     <font size="12"/>
    </textElement>
    <textFieldExpression class="java.lang.String"><![CDATA[$F{rollNo}]]></textFieldExpression>
   </textField>
  </band>
 </detail>
 <columnFooter>
  <band splitType="Stretch"/>
 </columnFooter>
 <pageFooter>
  <band splitType="Stretch"/>
 </pageFooter>
 <summary>
  <band splitType="Stretch"/>
 </summary>
</jasperReport>
 
</td></tr></tbody></table>
ckgupta's picture
18
Joined: May 25 2011 - 1:15am
Last seen: 7 years 10 months ago
0

Thanks for the more in-depth explanation.  It's clear to me what you're trying to accomplish.  That is a tough problem because if you remove that entire column, do the others slide to the left automatically?  I do not think that plain Jasper, by its self, would help with this type of report.

You could try to use dynamic jasper in conjunction with regular jasper.  Have you taken a look at that yet?  You'd have to write some custom code to see if that column has zero data but you could probably solve your problem easier with dynamic jasper.  Dynamic jasper has some nice capabilities that might make this task easier.  It has some capabilities that allow auto resizing of columns based on the overall report width which might make that column disappear altogether when it has no data and the others slide over.  Worth a poke around

http://dynamicjasper.com/

 



Post Edited by frankhassanabad at 11/25/2011 02:20
frankhassanabad's picture
Joined: Oct 25 2010 - 11:12am
Last seen: 4 years 11 months ago
0

Thanks frankhassanabad for your response.

I do not feel the need of sliding other colums to the left automatically, its ok if that column appears blank.

What my concern is that the column header should not be visible if there is no data for that field in any row.

for example

for data in dataBase as

-------------------------------

Name   Classs  RollNo

-------     ----------   -----------         

John      <null>    102

Mark       <null>        103

Robert    <null>     104

 

Report should look like

---------------------------------- 

Name                  RollNo

-------                    -----------

John                     102

Mark                     103

Robert                 104

ckgupta's picture
18
Joined: May 25 2011 - 1:15am
Last seen: 7 years 10 months ago
0

To get the null's to go away, just set the "blank when null" to true.  That's the more straight forward way than using a printWhenExpression:

isBlankWhenNull="true"

To get the column title to be null, well, that's a bit of a trick.  I say a bit of a trick because you don't know that every single piece of row of data for a particular column is empty until you run through the entire data set.  Jasper uses a streaming model so it's not going to know that answer until it's completely done rendering the report.  Which means for Jasper to add the feature...it would break its good memory management and streaming (at least as far as I understand it).

Now, depending on your data source (SQL, HQL, java POJO's, XML...etc...), if you can determine that all of the rows for a particular column is going to be empty and that the title should be null *before* you run the report, then you could create a set of boolean parameters for your report and pass in true or false for each column.  This might require you (at worst) to run through your data set completely once before running it through a second time using Jasper. 

So, if you're okay with that tradeoff, you could then use a printWhenExpression with a set of parameters to determine which column titles to print and not to print because then you'd know ahead of time which ones are going to be completely empty. 

That is, unless you're using a production DB and you have users constantly adding and deleting data.  Then, you could end up with a funky situation where betwen the first run to determine if all the rows for a column are blank and the second run where you're filling the report, a user adds some data.  hehe ;-)  Then I'd say try to use DB transactions for the connection you pass in to keep the data you're using consistent for the two runs.

Let me know if you think of a better solution.  That's the best I can dream up at the moment.  Happy reporting.



Post Edited by frankhassanabad at 11/25/2011 15:45
frankhassanabad's picture
Joined: Oct 25 2010 - 11:12am
Last seen: 4 years 11 months ago
2

Hi,

 

just create a boolean variable (e.g. isClassFieldFilled)....

as variable expression you do this:

$F{class} != null?Boolean.TRUE:$V{isClassFieldFilled}

as initial expression you do this:

Boolean.FALSE

now you transform you static label in column header to a textfield (in iReport you can do this with context menue)

set the following as text expression:

$V{isClassFieldFilled}?"Class":""

set the evaluation time of this textfield to REPORT

and voilá your column heading for class is just displayed if at least one record has a non null-value in field class assigned.

hth + regards from Germany

Christian

C-Box's picture
1142
Joined: Jul 19 2006 - 5:58pm
Last seen: 5 hours 28 min ago

i have the same problem and i need to make the column headers shrink to left when there no data.

Please need an answer ASAP. I appreciate your response.

swathi23 - 6 years 4 months ago
Feedback
randomness