How to convert selected row to table?

I have a field with the values:

CM45024,CM45025,CM45026 

I want to split this into several elements using subreport. My jrxml source:

 <field name="docIdNoGRN" class="java.lang.String"> <fieldDescription><![CDATA[docIdNoGRN]]></fieldDescription> </field> <textField isStretchWithOverflow="true" isBlankWhenNull="true"> <reportElement key="textField-53" x="311" y="1" width="88" height="15" uuid="2e040fd0-8fae-46e8-a845-fba421922992"/> <textElement textAlignment="Center"> <font size="10"/> </textElement> <textFieldExpression><![CDATA[($F{docIdNoGRN} != null && $F{docIdNoGRN}.toString().length() > 0) ? $F{docIdNoGRN} : " "]]> </textFieldExpression> </textField> 

A field can contain 3 or more elements, as it depends on the data. Instead of having one item in the report:

 Item No. Item ID. 1 CM45024,CM45025,CM45026 

I want to show it as follows:

 Item No. Item ID. 1 CM45024 2 CM45025 3 CM45026 

Im using TIBCO Jaspersoft® Studio Professional is a visual constructor for JasperReport 6.1.1.

+1
source share
1 answer

Here's how to do it using subreport, assuming your $F{docIdNoGRN} contains the string "CM45024, CM45025, CM45026"

  • Generate a data source to be transferred to the subordinate

     <subreport> <reportElement x="163" y="15" width="200" height="100" uuid="9d6660e0-094e-4df3-9acb-74c031350b10"/> <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource(java.util.Arrays.asList($F{docIdNoGRN}.split(",")))]]></dataSourceExpression> <subreportExpression><![CDATA[$P{SUBREPORT_DIR} + "your_subreport.jasper"]]></subreportExpression> </subreport> 

As you can see, I split(",") your String field receives an Array , which is converted to a List , and then passed to JRBeanCollectionDataSource

  1. Subregister setting, the trick for linking to our string uses _THIS as the field value

example: ( your_subreport.jrxml ):

 <?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="jTest_subreport2" language="groovy" pageWidth="555" pageHeight="802" columnWidth="555" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0" uuid="23d7765f-250f-4632-94c6-bbd218db3d11"> <field name="_THIS" class="java.lang.String"/> <detail> <band height="35" splitType="Stretch"> <textField> <reportElement x="0" y="0" width="100" height="20" uuid="716bb440-2692-4c58-a1b7-972aff240c67"/> <textFieldExpression><![CDATA[$V{REPORT_COUNT}]]></textFieldExpression> </textField> <textField> <reportElement x="100" y="0" width="100" height="20" uuid="e5464783-d74a-4405-9997-ddb1531c6e42"/> <textFieldExpression><![CDATA[$F{_THIS}]]></textFieldExpression> </textField> </band> </detail> </jasperReport> 

I added $V{REPORT_COUNT} to achieve the numbers (1,2,3).

The output will be

1 CM45024
2 CM45025
3 CM45026

Note. You can also use the jr: table component to achieve this if you want to avoid creating a sub-report (just define the table data source as described above).

+1
source

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


All Articles