How to check if the row is first on each page?

南笙酒味 提交于 2019-12-25 09:32:08

问题


I have list of data to print. I want to know if there is any way to find if the row is the first row in JasperReports's report for every page?


回答1:


This task can be solved with in several ways in addition of using JasperReports engine built-in variable PAGE_COUNT.

Using variable

We can create the variable with resetType="Page" for counting number of rows at page.

<variable name="counterOnPage" class="java.lang.Integer" resetType="Page" calculation="Sum">
    <variableExpression><![CDATA[1]]></variableExpression>
    <initialValueExpression><![CDATA[0]]></initialValueExpression>
</variable>

Datasource

The simple csv datasource will be good for samples.

First page. Row 1
First page. Row 2
First page. Row 3
First page. Row 4
First page. Row 5
Second page. Row 1
Second page. Row 2
Second page. Row 3
Second page. Row 4
Second page. Row 5
Third page. Row 1
Third page. Row 2
Third page. Row 3
Third page. Row 4
Third page. Row 5

The name of data adapter for this datasource in the example below is rows.csv. The name of a field is name

Sample report template

The height of report is enough for showing only 5 rows per page.

<?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="Using variable" pageWidth="250" pageHeight="75" whenNoDataType="AllSectionsNoDetail" columnWidth="210" leftMargin="20" rightMargin="20" topMargin="0" bottomMargin="0">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="rows.csv"/>
    <field name="name" class="java.lang.String"/>
    <variable name="counterOnPage" class="java.lang.Integer" resetType="Page" calculation="Sum">
        <variableExpression><![CDATA[1]]></variableExpression>
        <initialValueExpression><![CDATA[0]]></initialValueExpression>
    </variable>
    <detail>
        <band height="15">
            <textField>
                <reportElement x="0" y="0" width="180" height="15" uuid="1b535a7e-7a8e-4e44-91ff-c0b8415afcf1"/>
                <textFieldExpression><![CDATA[($V{counterOnPage} == 1) ? $F{name} + "!" : $F{name}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
</jasperReport>

For first row we will add the symbol "!" to the end of a string.

Output result

The result (pdf file) will be:

Using report's parameters map

Here is a little hack using parameters map ($P{REPORT_PARAMETERS_MAP}).

We can set some "flag" (instead of using variable) for marking the first row at page. We can put textField at pageHeader for setting value isFirst at report's Map, for example.

<textField>
    <reportElement x="0" y="0" width="100" height="1"/>
    <textFieldExpression><![CDATA[$P{REPORT_PARAMETERS_MAP}.put("isFirst", true)]]></textFieldExpression>
</textField>

-- we are initializing the value of our flag.

We should add check for isFirst value and change the value of this flag after first use. The fake textField will do this job

<textField>
    <reportElement x="180" y="0" width="0" height="15"/>
    <textFieldExpression><![CDATA[((Boolean) $P{REPORT_PARAMETERS_MAP}.put("isFirst", false)) ? "" : ""]]></textFieldExpression>
</textField>

-- we are reseting the value of our flag.

Sample report template

The datasource will be the same.

<?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="Using Map" pageWidth="250" pageHeight="76" whenNoDataType="AllSectionsNoDetail" columnWidth="210" leftMargin="20" rightMargin="20" topMargin="0" bottomMargin="0">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="rows.csv"/>
    <field name="name" class="java.lang.String"/>
    <pageHeader>
        <band height="1">
            <textField>
                <reportElement x="0" y="0" width="100" height="1"/>
                <textFieldExpression><![CDATA[$P{REPORT_PARAMETERS_MAP}.put("isFirst", true)]]></textFieldExpression>
            </textField>
        </band>
    </pageHeader>
    <detail>
        <band height="15">
            <textField>
                <reportElement x="0" y="0" width="180" height="15">
                </reportElement>
                <textFieldExpression><![CDATA[($P{REPORT_PARAMETERS_MAP}.get("isFirst") != null && ((Boolean) $P{REPORT_PARAMETERS_MAP}.get("isFirst")) == true) ? "! " + $F{name} + "!" : $F{name}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="180" y="0" width="0" height="15"/>
                <textFieldExpression><![CDATA[((Boolean) $P{REPORT_PARAMETERS_MAP}.put("isFirst", false)) ? "" : ""]]></textFieldExpression>
            </textField>
        </band>
    </detail>
</jasperReport>

For the first row we will add the symbol "!" to the begin and the end of a string.

Output result

The result (pdf file) will be:




回答2:


There is a built in variable name $V{PAGE_COUNT}

PAGE_COUNT - Built-in variable containing the number of records that were processed when generating the current page.

This variable starts at 1 for first record on page and counts to end of page, it will reset to 1 when new page is created.

This means that the first record on a page will have this variable ==1, you can for example use a printWhenExpression if you like to add something on first row.

<printWhenExpression><![CDATA[$V{PAGE_COUNT}.intValue()==1]]></printWhenExpression>


来源:https://stackoverflow.com/questions/41207456/how-to-check-if-the-row-is-first-on-each-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!