How to convert a , separated String to a table layout?

血红的双手。 提交于 2019-12-02 05:05:39

This is how it can be achieved using subreport, assuming that your $F{docIdNoGRN} contains the String "CM45024,CM45025,CM45026"

  1. Generate the data source that will be passed to the subreport

    <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 field String getting an Array that is converted to List and then passed in a JRBeanCollectionDataSource

  1. Setup the subreport, the trick to reference our String is using _THIS as 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 have added the $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 like to avoid generating a subreport (just define the table datasource as above)

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