Hello Guys! Today We learn how to design the Jasper Report by using Table Component. We will see this process of using table component in step by step process.

1. First We will open the blank iReport, then drag and drop the table component in the detail band portion on the open iReport screen.

Screenshot1

2. On the drop of table component on iReport window , you will get one dialog box on screen. In this dialog box, you have to select the empty table option, column numbers (According to your requirment) on first wizard and empty data source option on second wizard and you can select the table header,table footer,column header,column footer and other options on the final wizard.

screenShot2

3. Now you have to work on design of table and set the header of table by using “Static Text” component on Tool Box Of iReport and also set the record field by adding “Text Field” component.

4. After the completion of view part of table on Jasper Report, you have to decalre the field name same as to Domain field names on the .jrxml file as following manner.

    <subDataset name="Table Dataset 1" uuid="649fa5a2-1eef-4327-a0c8-872c6133833a">
		<field name="version" class="java.lang.Long"/>
		<field name="createdBy" class="com.assesmart.user.User"/>
		<field name="createdDate" class="java.sql.Timestamp"/>
		<field name="folder" class="com.assesmart.question.Folder"/>
		<field name="itemBank" class="java.lang.Long"/>
		<field name="questionType" class="com.assesmart.enumeration.QuestionType"/>
		<field name="questionHistory" class="java.util.List"/>
	</subDataset>

(This field value will be pass as a arraylist or map in the report definition(In JasperReportDef class object) on the generation time of JasperReport).Like this.

			def reportDef = new JasperReportDef(name: 'question_iReport.jrxml', reportData:questions,
                fileFormat: JasperExportFormat.PDF_FORMAT,parameters: params)

        	ByteArrayOutputStream byteArrayOutputStream=jasperService.generateReport(reportDef)
		

Note – Here questions is arraylist.

5. In the tag

<datasetParameter></datasetParameter>

You have to specify the “REPORT_DATA_SOUCE” on name attribute.

		<datasetRun subDataset="Table Dataset 1" uuid="39f8f0bf-8a2b-44f4-9a4c-0c873da79fba">
			<datasetParameter name="REPORT_DATA_SOURCE">
				<datasetParameterExpression><![CDATA[]]></datasetParameterExpression>
			</datasetParameter>	
          	<dataSourceExpression><![CDATA[$P{REPORT_DATA_SOURCE}]]></dataSourceExpression>
		</datasetRun>
	

6. In the next step you have to specify these field names on the given dynamic “Text Field” on the table view.

ScreenShot3

7. The complete code of .jrxml file is –

		<?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="new_table3" language="groovy" pageWidth="612" pageHeight="792" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="ced755c3-048f-4f70-bc4b-e022bbb71e9f">
	<property name="ireport.zoom" value="1.0"/>
	<property name="ireport.x" value="0"/>
	<property name="ireport.y" value="0"/>

<style name="table">
		<box>
			<pen lineWidth="1.0" lineColor="#000000"/>
		</box>
	</style>


<style name="table_TH" mode="Opaque" backcolor="#F0F8FF">
		<box>
			<pen lineWidth="0.5" lineColor="#000000"/>
		</box>
	</style>


<style name="table_CH" mode="Opaque" backcolor="#BFE1FF">
		<box>
			<pen lineWidth="0.5" lineColor="#000000"/>
		</box>
	</style>


<style name="table_TD" mode="Opaque" backcolor="#FFFFFF">
		<box>
			<pen lineWidth="0.5" lineColor="#000000"/>
		</box>
	</style>

	<subDataset name="Table Dataset 1" uuid="649fa5a2-1eef-4327-a0c8-872c6133833a">
		<field name="version" class="java.lang.Long"/>
		<field name="createdBy" class="com.assesmart.user.User"/>
		<field name="createdDate" class="java.sql.Timestamp"/>
		<field name="folder" class="com.assesmart.question.Folder"/>
		<field name="itemBank" class="java.lang.Long"/>
		<field name="questionType" class="com.assesmart.enumeration.QuestionType"/>
		<field name="questionHistory" class="java.util.List"/>
	</subDataset>
	<parameter name="absolutePath" class="java.lang.String"/>
	<background>
		<band splitType="Stretch"/>
	</background>
	<title>
		<band height="30" splitType="Stretch"/>
	</title>
	<pageHeader>
		<band height="47" splitType="Stretch">
			<staticText>
				<reportElement x="187" y="3" width="200" height="32" uuid="6ffebeb5-b3d6-439d-b44d-a8e0a2553ceb"/>
				<textElement textAlignment="Center" verticalAlignment="Middle">
					<font size="15" isUnderline="true"/>
				</textElement>
				<text><![CDATA[Report Of Question's]]></text>
			</staticText>
			<image>
				<reportElement x="0" y="0" width="143" height="32" uuid="a826db7a-4036-4373-ad36-0702262948a7"/>
				<imageExpression><![CDATA[$P{absolutePath} + "/logo.png"]]></imageExpression>
			</image>
		</band>
	</pageHeader>
	<detail>
		<band height="125" splitType="Stretch">
			<componentElement>
				<reportElement key="table" style="table" x="18" y="15" width="505" height="60" uuid="c7b69f4a-1f7d-4499-9e56-a6fe788e5282"/>
				<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="Table Dataset 1" uuid="39f8f0bf-8a2b-44f4-9a4c-0c873da79fba">
						<datasetParameter name="REPORT_DATA_SOURCE">
							<datasetParameterExpression><![CDATA[]]></datasetParameterExpression>
						</datasetParameter>
						<dataSourceExpression><![CDATA[$P{REPORT_DATA_SOURCE}]]></dataSourceExpression>
					</datasetRun>
					<jr:column width="97" uuid="27d89943-9756-4111-86ce-2eb445fe089e">
						<jr:columnHeader style="table_CH" height="30" rowSpan="1">
							<staticText>
								<reportElement x="0" y="0" width="97" height="30" uuid="2d117f76-8ae8-46d8-aaa6-ff14ac527a80"/>
								<textElement textAlignment="Center" verticalAlignment="Middle"/>
								<text><![CDATA[Item Bank]]></text>
							</staticText>
						</jr:columnHeader>
						<jr:detailCell style="table_TD" height="38" rowSpan="1">
							<textField>
								<reportElement x="0" y="0" width="97" height="38" uuid="67b3822c-1d33-4684-81ba-83335d18eb50"/>
								<textElement textAlignment="Center" verticalAlignment="Middle"/>
								<textFieldExpression><![CDATA[$F{itemBank}?$F{itemBank}.name:$F{folder}.topMostParent.name]]></textFieldExpression>
							</textField>
						</jr:detailCell>
					</jr:column>
					<jr:column width="99" uuid="e66be685-6675-46f7-98dd-b77611cc3eb8">
						<jr:columnHeader style="table_CH" height="30" rowSpan="1">
							<staticText>
								<reportElement x="0" y="0" width="99" height="30" uuid="d140e3b4-c146-4d0f-91c9-b9ebf6630531"/>
								<textElement textAlignment="Center" verticalAlignment="Middle"/>
								<text><![CDATA[Folder]]></text>
							</staticText>
						</jr:columnHeader>
						<jr:detailCell style="table_TD" height="38" rowSpan="1">
							<textField>
								<reportElement x="0" y="0" width="99" height="38" uuid="c0c5435d-d792-4704-98d0-fb29790aef0c"/>
								<textElement textAlignment="Center" verticalAlignment="Middle"/>
								<textFieldExpression><![CDATA[$F{folder}?$F{folder}.name:'----']]></textFieldExpression>
							</textField>
						</jr:detailCell>
					</jr:column>
					<jr:column width="85" uuid="f219707c-ccd8-4f18-945f-1f8ee3ba4984">
						<jr:columnHeader style="table_CH" height="30" rowSpan="1">
							<staticText>
								<reportElement x="0" y="0" width="85" height="30" uuid="9422de4d-301a-4091-8197-e21269960f2c"/>
								<textElement textAlignment="Center" verticalAlignment="Middle"/>
								<text><![CDATA[Question Type]]></text>
							</staticText>
						</jr:columnHeader>
						<jr:detailCell style="table_TD" height="38" rowSpan="1">
							<textField>
								<reportElement x="0" y="0" width="85" height="38" uuid="093a4e3b-7d1d-4cc4-85b6-461ed58129e7"/>
								<textElement textAlignment="Center" verticalAlignment="Middle"/>
								<textFieldExpression><![CDATA[$F{questionType}.getString()]]></textFieldExpression>
							</textField>
						</jr:detailCell>
					</jr:column>
					<jr:column width="94" uuid="0a1db393-d97c-406a-98c0-d0f098440e62">
						<jr:columnHeader style="table_CH" height="30" rowSpan="1">
							<staticText>
								<reportElement x="0" y="0" width="94" height="30" uuid="213d3e19-968a-475f-9e3a-99f00b6ae78e"/>
								<textElement textAlignment="Center" verticalAlignment="Middle"/>
								<text><![CDATA[Created By]]></text>
							</staticText>
						</jr:columnHeader>
						<jr:detailCell style="table_TD" height="38" rowSpan="1">
							<textField>
								<reportElement x="0" y="0" width="94" height="38" uuid="1cacbd03-9716-40f9-8c35-71d251098eab"/>
								<textElement textAlignment="Center" verticalAlignment="Middle"/>
								<textFieldExpression><![CDATA[$F{createdBy}.username]]></textFieldExpression>
							</textField>
						</jr:detailCell>
					</jr:column>
					<jr:column width="94" uuid="c22d790c-b0a9-4854-ac25-abc18f9939f1">
						<jr:columnHeader style="table_CH" height="30" rowSpan="1">
							<staticText>
								<reportElement x="0" y="0" width="94" height="30" uuid="772df5fc-09e2-408e-b1b2-4ced0d93d022"/>
								<textElement textAlignment="Center" verticalAlignment="Middle"/>
								<text><![CDATA[Created At]]></text>
							</staticText>
						</jr:columnHeader>
						<jr:detailCell style="table_TD" height="38" rowSpan="1">
							<textField>
								<reportElement x="0" y="0" width="94" height="38" uuid="4b05730c-31e1-4150-a88d-2250b84c5720"/>
								<textElement textAlignment="Center" verticalAlignment="Middle"/>
								<textFieldExpression><![CDATA[$F{createdDate}]]></textFieldExpression>
							</textField>
						</jr:detailCell>
					</jr:column>
					<jr:column width="74" uuid="760d454a-0e9b-4679-b741-50dc536e2de4">
						<jr:columnHeader style="table_CH" height="30" rowSpan="1">
							<staticText>
								<reportElement x="0" y="0" width="74" height="30" uuid="cba23dc2-7b19-4d56-b2f2-6db027185e7b"/>
								<textElement textAlignment="Center" verticalAlignment="Middle"/>
								<text><![CDATA[Version Number]]></text>
							</staticText>
						</jr:columnHeader>
						<jr:detailCell style="table_TD" height="38" rowSpan="1">
							<textField>
								<reportElement x="0" y="0" width="74" height="38" uuid="14afc71a-8264-41be-8fcf-fb980129e4fe"/>
								<textElement textAlignment="Center" verticalAlignment="Middle"/>
								<textFieldExpression><![CDATA[$F{questionHistory}.size()]]></textFieldExpression>
							</textField>
						</jr:detailCell>
					</jr:column>
				</jr:table>
			</componentElement>
		</band>
	</detail>
	<columnFooter>
		<band height="45" splitType="Stretch"/>
	</columnFooter>
	<pageFooter>
		<band height="54" splitType="Stretch">
			<textField pattern="dd/MM/yyyy">
				<reportElement x="18" y="19" width="100" height="20" uuid="e3f8ef61-7915-43ad-8b87-942553ca378d"/>
				<textFieldExpression><![CDATA[new java.util.Date()]]></textFieldExpression>
			</textField>
			<textField>
				<reportElement x="213" y="19" width="80" height="20" uuid="4136705b-1f0f-4290-9fe4-ab801acbf617"/>
				<textElement textAlignment="Right"/>
				<textFieldExpression><![CDATA["Page "+$V{PAGE_NUMBER}+" of"]]></textFieldExpression>
			</textField>
			<textField evaluationTime="Report">
				<reportElement x="293" y="19" width="40" height="20" uuid="060a3d23-c293-45c8-b028-ebab34c82b51"/>
				<textFieldExpression><![CDATA[" " + $V{PAGE_NUMBER}]]></textFieldExpression>
			</textField>
			<textField pattern="h.mm a">
				<reportElement x="459" y="19" width="100" height="20" uuid="5acb2609-1b85-4616-aca9-16be316490fc"/>
				<textFieldExpression><![CDATA[new java.util.Date()]]></textFieldExpression>
			</textField>
		</band>
	</pageFooter>

<summary>
		<band height="42" splitType="Stretch"/>
	</summary>

</jasperReport>

	

8. Final view will be pdf file as Jasper Report.

last

Hope this help.. 🙂