JasperReports: how to determine if a field will be moved to the next page

I create a report with contacts that span different pages.

I used to have isSplitAllowed set to true. But I do not want the contact divided into different pages. So I set it to false, the report layout looked much better. However, he introduced a new problem:

In my heading, I type the name of the first person and the last person on the page. When a contact moves to the next page, it is still being processed on the current page. So let me say that Bob should have been at the bottom of the first page. But to prevent it from splitting, Bob is now the first element of the second page. However, in the title of my first page, I still have Alice ... Bob (where Alice is the first person on 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 is the last person of the 2nd page).

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

where pageFirstItem:

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

and pageLastItem:

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

+4
source share
1 answer

I applied the working example example below, based on iReport 3.0.5. You can test it by running it with isSplitAllowed enabled and disabled. The problem is that the detailed record is processed on page i, even if isSplitAllowed="false" causes the record to print on page i + 1.

Summary:

  • For firstItem , no calculations are needed. Just put the Name field in the header.
  • For lastItem you need to keep the name processed using the prop variable of type java.util.Property . This is done by placing in the details section a fictitious height 0, textField with the following value prop.setProperty("lastSavedName", $F{Name}) . This textField ensures that the name is retained only after it is printed. A.
  • Then put the value from prop, prop.getProperty("lastSavedName") in the header using 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> 
+1
source

Source: https://habr.com/ru/post/1394048/


All Articles