Sunday 22 April 2018

Tip : Return variable(s) from Table component Dataset to display on Title band of report in Japsersoft reports

Hi folks, 

A table component in Jaspersoft requires a data set to be created and it can be done as usual but what if you want to display the calculations from table component outside of it's area on the same report or may be in some other report. 

We use "Return variables" concept on data sets that are being used by the components. For instance, below image is an example to display the avg shelf height and total shelf depth columns of table component outside of its(table) area. i.e, display the avg and total, say in "Title" band or say at some specific portion in Summary band. 

(Click on image to get best view of the content written over it)

To accomplish it, create variables in Dataset of the table component. 

For instance, Calculating Avg Shelf 

1) Create a variable in table data set, say "tableVarShelfHeight" of type Double in my case and the Calculation is Average and the Expression is $F{shelf_height} and IncrementType as None and Reset Type as Report.

2) Create a another variable in report variables, say "reportVarShelfHeight", of type Double and with Calculation as "No Calculation Function" and IncrementType as None and Reset Type as Report


3) Now, click on the Table component, and in its properties be on "Dataset" section and then click on the button called "Return Variables". In the pop-up window opened click on "Add" button to add then give the values for "From Variable" as "tableVarShelfHeight" and "To Variable" as "reportVarShelfHeight".

4) Now, from the report outline, and from the variables drag on drop the "reportVarShelfHeight" to the "Title" band and change its "Evaluation Time" as "Report" (by default it would be "None")

5) Follow, the same steps to create tableVarShelfDepth variable for total. 

This way, a calculation created in data sets of table component can be utilized on Title band of the same report. 

Want to look at the explained example instantly, copy and paste below JRXML in your test JRXML and observe the implementation.!

Hope it would be some help to you.!

Until Next Tip/Post, 
Sadakar Pochampalli 

JRXML: (Works in Jaspersoft Studio 6.4.2 professional)
<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.4.2.final using JasperReports Library version 6.4.1  -->
<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="TableComponentReturnVariable" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="076eb3bf-3e45-4383-aab2-41f7a7ab9677">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="foodmart"/>
<property name="com.jaspersoft.studio.data.sql.tables" value=""/>
<property name="ireport.jasperserver.url" value="http://localhost:8080/jasperserver-pro/"/>
<property name="ireport.jasperserver.user" value="superuser"/>
<property name="ireport.jasperserver.report.resource" value="/EarthlySystems/TableComponentReturnVariable_files/main_jrxml"/>
<property name="ireport.jasperserver.reportUnit" value="/EarthlySystems/TableComponentReturnVariable"/>
<style name="Table_TH" mode="Opaque" backcolor="#F0F8FF">
<box>
<pen lineWidth="0.5" lineColor="#000000"/>
<topPen lineWidth="0.5" lineColor="#000000"/>
<leftPen lineWidth="0.5" lineColor="#000000"/>
<bottomPen lineWidth="0.5" lineColor="#000000"/>
<rightPen lineWidth="0.5" lineColor="#000000"/>
</box>
</style>
<style name="Table_CH" mode="Opaque" backcolor="#BFE1FF">
<box>
<pen lineWidth="0.5" lineColor="#000000"/>
<topPen lineWidth="0.5" lineColor="#000000"/>
<leftPen lineWidth="0.5" lineColor="#000000"/>
<bottomPen lineWidth="0.5" lineColor="#000000"/>
<rightPen lineWidth="0.5" lineColor="#000000"/>
</box>
</style>
<style name="Table_TD" mode="Opaque" backcolor="#FFFFFF">
<box>
<pen lineWidth="0.5" lineColor="#000000"/>
<topPen lineWidth="0.5" lineColor="#000000"/>
<leftPen lineWidth="0.5" lineColor="#000000"/>
<bottomPen lineWidth="0.5" lineColor="#000000"/>
<rightPen lineWidth="0.5" lineColor="#000000"/>
</box>
</style>
<subDataset name="Dataset1" uuid="13c73a68-84f9-4afb-8d85-c363df55edd5">
<property name="com.jaspersoft.studio.data.sql.tables" value=""/>
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="foodmart"/>
<queryString language="SQL">
<![CDATA[SELECT * FROM product limit 10]]>
</queryString>
<field name="product_class_id" class="java.lang.Integer"/>
<field name="product_id" class="java.lang.Integer"/>
<field name="brand_name" class="java.lang.String"/>
<field name="product_name" class="java.lang.String"/>
<field name="sku" class="java.lang.Long"/>
<field name="srp" class="java.math.BigDecimal"/>
<field name="gross_weight" class="java.lang.Float"/>
<field name="net_weight" class="java.lang.Float"/>
<field name="recyclable_package" class="java.lang.Boolean"/>
<field name="low_fat" class="java.lang.Boolean"/>
<field name="units_per_case" class="java.lang.Integer"/>
<field name="cases_per_pallet" class="java.lang.Integer"/>
<field name="shelf_width" class="java.lang.Double"/>
<field name="shelf_height" class="java.lang.Double"/>
<field name="shelf_depth" class="java.lang.Double"/>
<variable name="tableVarShelfHeight" class="java.lang.Double" calculation="Average">
<variableExpression><![CDATA[$F{shelf_height}]]></variableExpression>
</variable>
<variable name="tableVarShelfDepth" class="java.lang.Double" calculation="Sum">
<variableExpression><![CDATA[$F{shelf_depth}]]></variableExpression>
</variable>
</subDataset>
<queryString>
<![CDATA[select 1 as one]]>
</queryString>
<field name="one" class="java.lang.Integer"/>
<variable name="reportVarShelfHeight" class="java.lang.Double"/>
<variable name="reportVarShelfDepth" class="java.lang.Double"/>
<background>
<band splitType="Stretch"/>
</background>
<summary>
<band height="155" splitType="Stretch">
<componentElement>
<reportElement x="0" y="95" width="555" height="60" uuid="fb230941-894f-49b6-a5db-33fd14e143ca">
<property name="com.jaspersoft.studio.layout" value="com.jaspersoft.studio.editor.layout.VerticalRowLayout"/>
<property name="com.jaspersoft.studio.table.style.table_header" value="Table_TH"/>
<property name="com.jaspersoft.studio.table.style.column_header" value="Table_CH"/>
<property name="com.jaspersoft.studio.table.style.detail" value="Table_TD"/>
</reportElement>
<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="Dataset1" uuid="e267dc57-7f5a-496a-afbe-4c501f7641c0">
<connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
<returnValue fromVariable="tableVarShelfHeight" toVariable="reportVarShelfHeight"/>
<returnValue fromVariable="tableVarShelfDepth" toVariable="reportVarShelfDepth"/>
</datasetRun>
<jr:column width="150" uuid="72c24377-f2d4-475b-a4e5-67e44a84e77e">
<property name="com.jaspersoft.studio.components.table.model.column.name" value="Column1"/>
<property name="com.jaspersoft.studio.unit.width" value="pixel"/>
<jr:columnHeader style="Table_CH" height="30">
<property name="com.jaspersoft.studio.unit.width" value="px"/>
<staticText>
<reportElement x="0" y="0" width="150" height="30" uuid="8fc092e9-c4c6-4852-b9f9-f0ba87058672"/>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font size="12" isBold="true"/>
</textElement>
<text><![CDATA[Brand Name]]></text>
</staticText>
</jr:columnHeader>
<jr:detailCell style="Table_TD" height="30">
<textField>
<reportElement x="0" y="0" width="150" height="30" uuid="7b7ca06b-b091-49e4-88b3-2933d406b307"/>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[$F{brand_name}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
<jr:column width="160" uuid="c0ff827a-b508-4cb3-a56e-907dd5c6541b">
<property name="com.jaspersoft.studio.components.table.model.column.name" value="Column2"/>
<jr:columnHeader style="Table_CH" height="30">
<staticText>
<reportElement x="0" y="0" width="160" height="30" uuid="cfe5cfa6-a8c0-4e21-be9b-905f5a4c3b5f"/>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font size="12" isBold="true"/>
</textElement>
<text><![CDATA[Product Name]]></text>
</staticText>
</jr:columnHeader>
<jr:detailCell style="Table_TD" height="30">
<textField>
<reportElement x="0" y="0" width="160" height="30" uuid="79e67174-3f79-4bd2-9431-382caef06859"/>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[$F{product_name}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
<jr:column width="100" uuid="d181aa89-109e-4611-a88d-b02987b5f843">
<property name="com.jaspersoft.studio.components.table.model.column.name" value="Column3"/>
<jr:columnHeader style="Table_CH" height="30">
<staticText>
<reportElement x="0" y="0" width="100" height="30" uuid="eccf9427-0a77-42d2-843c-45240add3380"/>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font size="12" isBold="true"/>
</textElement>
<text><![CDATA[Shelf Height]]></text>
</staticText>
</jr:columnHeader>
<jr:detailCell style="Table_TD" height="30">
<textField>
<reportElement x="0" y="0" width="100" height="30" uuid="2bcee921-da1c-446b-b0d6-f7eabc9d1e09"/>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[$F{shelf_height}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
<jr:column width="145" uuid="bdaa807a-f7ad-4012-b785-2043e2f8b5ed">
<property name="com.jaspersoft.studio.components.table.model.column.name" value="Column4"/>
<jr:columnHeader style="Table_CH" height="30">
<staticText>
<reportElement x="0" y="0" width="145" height="30" uuid="b49b7a55-8b66-4008-9fa5-b8c7eef487c4"/>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font size="12" isBold="true"/>
</textElement>
<text><![CDATA[Shelf Depth]]></text>
</staticText>
</jr:columnHeader>
<jr:detailCell style="Table_TD" height="30">
<textField>
<reportElement x="0" y="0" width="145" height="30" uuid="eaf798a6-92db-4d2a-bae4-cbb0a3fc3df1"/>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[$F{shelf_depth}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
</jr:table>
</componentElement>
<textField evaluationTime="Report">
<reportElement x="310" y="0" width="110" height="30" uuid="ae60e175-5e2b-41a9-97e4-e56d03e04879"/>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font isBold="true"/>
</textElement>
<textFieldExpression><![CDATA[$V{reportVarShelfHeight}]]></textFieldExpression>
</textField>
<textField evaluationTime="Report">
<reportElement x="420" y="0" width="135" height="30" uuid="1ddf8337-b8ed-4d7f-a8b6-b3254ef08f1e"/>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font isBold="true"/>
</textElement>
<textFieldExpression><![CDATA[$V{reportVarShelfDepth}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="310" y="30" width="110" height="30" uuid="3400a3c6-b92f-4191-9a0f-c9e4277f6789"/>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font size="12" isBold="true"/>
</textElement>
<textFieldExpression><![CDATA["Avg Shelf Height"]]></textFieldExpression>
</textField>
<textField>
<reportElement x="420" y="30" width="135" height="30" uuid="effa8104-837d-4dc8-b08a-76645fed513c"/>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font size="12" isBold="true"/>
</textElement>
<textFieldExpression><![CDATA["Total. Shelf Depth"]]></textFieldExpression>
</textField>
</band>
</summary>
</jasperReport>

No comments:

Post a Comment