Jump to content
We've recently updated our Privacy Statement, available here ×

Split a field on the basis of delimiter and print all the strings


sneha.sinha_1

Recommended Posts

Suppose I have got a dataset from query as-

ColumnAColumnB
ColumnC 
ColumnD
A1B1C1

D11,D21,D31

A2B2C2

D21,D22,D32

 

I am trying to split it like following-

ColumnAColumnBColumnCColumnD
A1B1C1D11
A1B1C1D21
A1B1C1D31
A2B2C2D21
A2B2C2D22
A2B2C2D32

Can anyone suggest anything.I have tried $F{ColumnD}.split(",") but that returns an array of string ,which I dont know how to traverse in Jasper.RThe length of ColumnD is dynamic.

Link to comment
Share on other sites

  • Replies 2
  • Created
  • Last Reply

Top Posters In This Topic

You could 1. create an Array of Strings based on your Field:

$F{ColumnD}.split(",") [/code]

and 2. create an ArrayList of that:

Arrays.asList($F{ColumnD}.split(",") [/code]

and 3. use this list for an ListElement or even a SubReport as a JRBeanCollectionDataSource using finally this as DataSourceExpression:

new JRBeanCollectionDataSource(Arrays.asList($F{ColumnD}.split(",") ))[/code]

in your List or SubReport you must create a Field named "_THIS" of type String that you can place within the Listelement or the detail of your SubReport.

just tried it out with following parameter in a new empty (main) record:

<parameter name="MyStrings" class="java.util.List" nestedType="java.lang.String" isForPrompting="false">        <defaultValueExpression><![CDATA[Arrays.asList("Record1", "2. Record","and the third one")]]></defaultValueExpression>    </parameter>[/code]

and this for SubReport expression:

<subreport>                <reportElement x="0" y="10" width="545" height="60" uuid="7f7e06fb-1788-4fdc-a1a3-3f5dd3745533"/>                <dataSourceExpression><![CDATA[new JRBeanCollectionDataSource($P{MyStrings})]]></dataSourceExpression>                <subreportExpression><![CDATA["StringListAsDataSource_Sub.jasper"]]></subreportExpression>            </subreport>[/code]

and this as whole SubReport:

<?xml version="1.0" encoding="UTF-8"?><!-- Created with Jaspersoft Studio version 6.6.0.final using JasperReports Library version 6.5.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="StringListAsDataSource_Sub" pageWidth="595" pageHeight="842" columnWidth="595" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0" uuid="9d9ea5fd-fadc-4890-827f-369ee64374e3">    <property name="com.jaspersoft.studio.unit." value="pixel"/>    <property name="com.jaspersoft.studio.unit.pageHeight" value="pixel"/>    <property name="com.jaspersoft.studio.unit.pageWidth" value="pixel"/>    <property name="com.jaspersoft.studio.unit.topMargin" value="pixel"/>    <property name="com.jaspersoft.studio.unit.bottomMargin" value="pixel"/>    <property name="com.jaspersoft.studio.unit.leftMargin" value="pixel"/>    <property name="com.jaspersoft.studio.unit.rightMargin" value="pixel"/>    <property name="com.jaspersoft.studio.unit.columnWidth" value="pixel"/>    <property name="com.jaspersoft.studio.unit.columnSpacing" value="pixel"/>    <queryString>        <![CDATA[]]>    </queryString>    <field name="_THIS" class="java.lang.String"/>    <background>        <band splitType="Stretch"/>    </background>    <detail>        <band height="30" splitType="Stretch">            <textField>                <reportElement x="0" y="0" width="440" height="30" uuid="2beea897-e04d-4bc2-aa50-1238ad9aef85">                    <property name="com.jaspersoft.studio.spreadsheet.connectionID" value="837e5d94-d9e1-4f14-af77-3dc9369ce311"/>                </reportElement>                <textFieldExpression><![CDATA[$F{_THIS}]]></textFieldExpression>            </textField>        </band>    </detail></jasperReport>[/code]

 

hth + regards

C-Box

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...