user1181031
user1181031

Reputation: 71

JasperReports: how to tell if a field is pushed onto next page

I am generating a report with contacts that span across different pages.

Previously I had isSplitAllowed set to "true". But I don't want a contact to split across different pages. So I set it to "false", the layout of the report looked much better. However it introduced a new problem:

In my header, I print the name of the first person and last person of the page. When a contact gets pushed to the next page, it is still processed in the current page. So let's say Bob was supposed to be at the bottom of the first page. But to prevent it from splitting, Bob is now the first element in the second page. However on the header of my first page I still have Alice...Bob (where Alice is the first person of the first page). And on my second page I still have Brenda...Doug (Where Brenda is now the 2nd person of the 2nd page, and Doug last person of 2nd page).

My output at the header is [$V{pageFirstItem} + "..." + $V{pageLastItem}]]>

where pageFirstItem is:

 <variable name="pageFirstItem" class="java.lang.String" resetType="Page" calculation="First">
        <variableExpression><![CDATA[$F{lastName}]]></variableExpression>
        <initialValueExpression><![CDATA[$F{lastName}]]></initialValueExpression>
    </variable>

and pageLastItem is:

<variable name="pageLastItem" class="java.lang.String" resetType="Report" calculation="Nothing">
  <variableExpression><![CDATA[$F{lastName}]]></variableExpression>

Upvotes: 4

Views: 3903

Answers (1)

user845279
user845279

Reputation: 2804

I've attached a working sample workaround below based on iReport 3.0.5. You can test it by running it with isSplitAllowed enabled and disabled. The problem is that a detail record is processed on page i even if isSplitAllowed="false" forces the record to be printed on page i+1.

Summary:

  1. For firstItem, no calculation is needed. Just put the Name field in the header.
  2. For lastItem, you need to save the Name processed using a variable, prop, of type java.util.Property. This is done by putting a dummy, height of 0, textField in the detail section with the following value prop.setProperty("lastSavedName", $F{Name}). This textField makes sure that the name is saved only after it is printed.
  3. Then put the value from prop, prop.getProperty("lastSavedName"), in the header with the evaluationTime="Page".

    <?xml version="1.0" encoding="UTF-8"  ?>
    <!-- Created with iReport - A designer for JasperReports -->
    <!DOCTYPE jasperReport PUBLIC "//JasperReports//DTD Report Design//EN" "http://jasperreports.sourceforge.net/dtds/jasperreport.dtd">
    <jasperReport
             name="Untitled_report_1"
             columnCount="1"
             printOrder="Vertical"
             orientation="Portrait"
             pageWidth="595"
             pageHeight="842"
             columnWidth="535"
             columnSpacing="0"
             leftMargin="30"
             rightMargin="30"
             topMargin="20"
             bottomMargin="20"
             whenNoDataType="NoPages"
             isFloatColumnFooter="true"
             isTitleNewPage="false"
             isSummaryNewPage="true">
        <property name="ireport.zoom" value="1.0" />
        <property name="ireport.x" value="0" />
        <property name="ireport.y" value="0" />
        <property name="ireport.scriptlethandling" value="0" />
        <property name="ireport.encoding" value="UTF-8" />
        <import value="java.util.*" />
        <import value="net.sf.jasperreports.engine.*" />
        <import value="net.sf.jasperreports.engine.data.*" />
    
        <queryString><![CDATA[select 1 as id, 'Name 1' as name from dual union all
    select 2 as id, 'Name 2' as name from dual union all
    select 3 as id, 'Name 3' as name from dual union all
    select 4 as id, 'Name 4' as name from dual union all
    select 5 as id, 'Name 5' as name from dual union all
    select 6 as id, 'Name 6' as name from dual union all
    select 7 as id, 'Name 7' as name from dual union all
    select 8 as id, 'Name 8' as name from dual union all
    select 9 as id, 'Name 9' as name from dual union all
    select 10 as id, 'Name 10' as name from dual union all
    select 11 as id, 'Name 11' as name from dual union all
    select 12 as id, 'Name 12' as name from dual union all
    select 13 as id, 'Name 13' as name from dual union all
    select 14 as id, 'Name 14' as name from dual union all
    select 15 as id, 'Name 15' as name from dual union all
    select 16 as id, 'Name 16' as name from dual union all
    select 17 as id, 'Name 17' as name from dual union all
    select 18 as id, 'Name 18' as name from dual union all
    select 19 as id, 'Name 19' as name from dual union all
    select 20 as id, 'Name 20' as name from dual union all
    select 21 as id, 'Name 21' as name from dual union all
    select 22 as id, 'Name 22' as name from dual union all
    select 23 as id, 'Name 23' as name from dual union all
    select 24 as id, 'Name 24' as name from dual union all
    select 25 as id, 'Name 25' as name from dual union all
    select 26 as id, 'Name 26' as name from dual union all
    select 27 as id, 'Name 27' as name from dual union all
    select 28 as id, 'Name 28' as name from dual]]></queryString>
    
        <field name="ID" class="java.math.BigDecimal"/>
        <field name="NAME" class="java.lang.String"/>
    
        <variable name="prop" class="java.util.Properties" resetType="Report" calculation="System">
            <initialValueExpression><![CDATA[new Properties()]]></initialValueExpression>
        </variable>
            <background>
                <band height="0"  isSplitAllowed="true" >
                </band>
            </background>
            <title>
                <band height="0"  isSplitAllowed="true" >
                </band>
            </title>
            <pageHeader>
                <band height="16"  isSplitAllowed="true" >
                    <textField isStretchWithOverflow="false" isBlankWhenNull="false" evaluationTime="Now" hyperlinkType="None"  hyperlinkTarget="Self" >
                        <reportElement
                            x="0"
                            y="0"
                            width="96"
                            height="16"
                            key="textField-3"/>
                        <box></box>
                        <textElement>
                            <font/>
                        </textElement>
                    <textFieldExpression   class="java.lang.String"><![CDATA["First  " + $F{ID}]]></textFieldExpression>
                    </textField>
                    <textField isStretchWithOverflow="false" isBlankWhenNull="false" evaluationTime="Page" hyperlinkType="None"  hyperlinkTarget="Self" >
                        <reportElement
                            x="96"
                            y="0"
                            width="100"
                            height="16"
                            key="textField-4"/>
                        <box></box>
                        <textElement>
                            <font/>
                        </textElement>
                    <textFieldExpression   class="java.lang.String"><![CDATA["Last  " + $V{prop}.getProperty("abcd")]]></textFieldExpression>
                    </textField>
                </band>
            </pageHeader>
            <columnHeader>
                <band height="20"  isSplitAllowed="true" >
                    <staticText>
                        <reportElement
                            x="0"
                            y="0"
                            width="96"
                            height="20"
                            key="staticText-2"/>
                        <box></box>
                        <textElement>
                            <font/>
                        </textElement>
                    <text><![CDATA[CustomerName]]></text>
                    </staticText>
                    <staticText>
                        <reportElement
                            x="96"
                            y="0"
                            width="100"
                            height="20"
                            key="staticText-3"/>
                        <box></box>
                        <textElement>
                            <font/>
                        </textElement>
                    <text><![CDATA[Workorderid]]></text>
                    </staticText>
                </band>
            </columnHeader>
            <detail>
                <band height="18"  isSplitAllowed="true" >
                    <textField isStretchWithOverflow="true" isBlankWhenNull="false" evaluationTime="Now" hyperlinkType="None"  hyperlinkTarget="Self" >
                        <reportElement
                            x="0"
                            y="0"
                            width="96"
                            height="18"
                            key="textField"/>
                        <box></box>
                        <textElement>
                            <font/>
                        </textElement>
                    <textFieldExpression   class="java.lang.String"><![CDATA[$F{NAME}+"This\nwill\ncause\na\nsplit."]]></textFieldExpression>
                    </textField>
                    <textField isStretchWithOverflow="false" pattern="##0.00" isBlankWhenNull="false" evaluationTime="Now" hyperlinkType="None"  hyperlinkTarget="Self" >
                        <reportElement
                            x="96"
                            y="0"
                            width="100"
                            height="18"
                            key="textField"/>
                        <box></box>
                        <textElement>
                            <font/>
                        </textElement>
                    <textFieldExpression   class="java.math.BigDecimal"><![CDATA[$F{ID}]]></textFieldExpression>
                    </textField>
                    <textField isStretchWithOverflow="false" isBlankWhenNull="false" evaluationTime="Band" hyperlinkType="None"  hyperlinkTarget="Self" >
                        <reportElement
                            x="196"
                            y="0"
                            width="339"
                            height="0"
                            key="textField-5"
                            isRemoveLineWhenBlank="true"/>
                        <box></box>
                        <textElement>
                            <font/>
                        </textElement>
                    <textFieldExpression   class="java.lang.String"><![CDATA[$V{prop}.setProperty("abcd", String.valueOf($F{ID}))]]></textFieldExpression>
                    </textField>
                </band>
            </detail>
            <columnFooter>
                <band height="0"  isSplitAllowed="true" >
                </band>
            </columnFooter>
            <pageFooter>
                <band height="0"  isSplitAllowed="true" >
                </band>
            </pageFooter>
            <summary>
                <band height="0"  isSplitAllowed="true" >
                </band>
            </summary>
    </jasperReport>
    

Upvotes: 1

Related Questions