Jaspersoft jsonql迭代列表以获取下一个元素

oalqel3c  于 2023-02-17  发布在  其他
关注(0)|答案(1)|浏览(136)

我的报告使用jaspersoft studio 6.6.0,我的报告使用json作为数据源,在我的报告中有一个字段根据当前端口的条件显示下一个端口名称,但我无法显示,我的json是这样的

"routingLocs": [
            {
                "callOrder": 1,
                "locCode": "ZWTHJ",
                "isCurrentLoc": "N"
            },
            {
                "callOrder": 2,
                "locCode": "TRYAR",
                "isCurrentLoc": "Y"
            },
            {
                "callOrder": 3,
                "locCode": "AUABP",
                "isCurrentLoc": "N"
            },
            {
                "callOrder": 4,
                "locCode": "RAJPI",
                "isCurrentLoc": "N"
            }
        ]

现在,我必须显示对象的locCode,该对象紧挨着isCurrentLoc =“Y”的对象,为此,我创建了一个字段,表达式为
<![CDATA[routingLocs..locCode(^.isCurrentLoc == "Y")]]>
此表达式将打印“TRYAR”而不是“AUABP”
我在JRXML中用于显示值的字段定义为

<field name="nextPort" class="java.lang.String">
        <property name="net.sf.jasperreports.jsonql.field.expression">
            <![CDATA[routingLocs..locCode(^.isCurrentLoc == "Y")]]>
        </property>
        <fieldDescription><![CDATA[nextPort]]></fieldDescription>
    </field>

请帮我修正这个表达式
提前感谢您的任何支持

cnwbcb6i

cnwbcb6i1#

使用jsonql查询无法访问同级元素,但可以在单独的subDataset中过滤JSON以达到所需的效果。如果JSON数据中存在某种顺序,例如callOrder的升序,则可以使用过滤查询开始创建主数据集,如下所示:

<queryString language="jsonql">
    <![CDATA[routingLocs.*(isCurrentLoc == "Y")]]>
</queryString>

然后创建一个接受MAIN_CALL_ORDER参数的subDataset,它将使用与main相同的JSON数据:

<subDataset name="nextPortDataset" uuid="a8b8a64f-4c6b-4c28-91db-e10f28cef52c">
    <property name="net.sf.jasperreports.data.adapter" value="./DataAdapter.xml"/>
    <parameter name="MAIN_CALL_ORDER" class="java.lang.Integer"/>
    <queryString language="jsonql">
        <![CDATA[routingLocs.*(callOrder > $P{MAIN_CALL_ORDER})[0]]]>
    </queryString>
    <field name="locCode" class="java.lang.String">
        <property name="net.sf.jasperreports.json.field.expression" value="locCode"/>
        <fieldDescription><![CDATA[locCode]]></fieldDescription>
    </field>    
</subDataset>

在这里,查询将生成callOrder大于主数据集发送的callOrder的第一个项目,然后这个数据集应该链接到一个列表,该列表将被传递参数,如下所示:

<jr:list xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd" printOrder="Vertical">
    <datasetRun subDataset="nextPortDataset" uuid="55c7173b-0b04-4d4f-bd80-48cfe1ef620c">
        <datasetParameter name="MAIN_CALL_ORDER">
            <datasetParameterExpression><![CDATA[$F{mainCallOrder}]]></datasetParameterExpression>
        </datasetParameter>
    </datasetRun>
    <jr:listContents height="30" width="460">
        <textField>
            <reportElement x="230" y="0" width="115" height="30" uuid="213ab052-9cdc-419a-8d3d-45db3bf6ef90"/>
            <textFieldExpression><![CDATA[$F{locCode}]]></textFieldExpression>
        </textField>        
    </jr:listContents>
</jr:list>

一个基本的工作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="Report" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="e9573346-c7d0-4b7a-a838-d2ff5b7b9512">
    <property name="net.sf.jasperreports.data.adapter" value="./DataAdapter.xml"/>
    <subDataset name="nextPortDataset" uuid="a8b8a64f-4c6b-4c28-91db-e10f28cef52c">
        <property name="net.sf.jasperreports.data.adapter" value="./DataAdapter.xml"/>
        <parameter name="MAIN_CALL_ORDER" class="java.lang.Integer"/>
        <queryString language="jsonql">
            <![CDATA[routingLocs.*(callOrder > $P{MAIN_CALL_ORDER})[0]]]>
        </queryString>
        <field name="callOrder" class="java.lang.Integer">
            <property name="net.sf.jasperreports.json.field.expression" value="callOrder"/>
            <fieldDescription><![CDATA[callOrder]]></fieldDescription>
        </field>
        <field name="locCode" class="java.lang.String">
            <property name="net.sf.jasperreports.json.field.expression" value="locCode"/>
            <fieldDescription><![CDATA[locCode]]></fieldDescription>
        </field>
        <field name="isCurrentLoc" class="java.lang.String">
            <property name="net.sf.jasperreports.json.field.expression" value="isCurrentLoc"/>
            <fieldDescription><![CDATA[isCurrentLoc]]></fieldDescription>
        </field>
    </subDataset>
    <queryString language="jsonql">
        <![CDATA[routingLocs.*(isCurrentLoc == "Y")]]>
    </queryString>
    <field name="mainCallOrder" class="java.lang.Integer">
        <property name="net.sf.jasperreports.jsonql.field.expression" value="callOrder"/>
        <fieldDescription><![CDATA[mainCallOrder]]></fieldDescription>
    </field>
    <field name="mainLocCode" class="java.lang.String">
        <property name="net.sf.jasperreports.jsonql.field.expression" value="locCode"/>
        <fieldDescription><![CDATA[mainLocCode]]></fieldDescription>
    </field>
    <field name="mainIsCurrentLoc" class="java.lang.String">
        <property name="net.sf.jasperreports.jsonql.field.expression" value="isCurrentLoc"/>
        <fieldDescription><![CDATA[mainIsCurrentLoc]]></fieldDescription>
    </field>
    <background>
        <band splitType="Stretch"/>
    </background>
    <title>
        <band height="79" splitType="Stretch"/>
    </title>
    <pageHeader>
        <band height="35" splitType="Stretch"/>
    </pageHeader>
    <columnHeader>
        <band height="32" splitType="Stretch">
            <staticText>
                <reportElement x="115" y="0" width="115" height="30" uuid="4ec73015-1c44-441c-a80e-bff448923a29">
                    <property name="com.jaspersoft.studio.spreadsheet.connectionID" value="bff05bbe-0c73-4e55-adec-461fd0f2ebff"/>
                </reportElement>
                <text><![CDATA[callOrder]]></text>
            </staticText>
            <staticText>
                <reportElement x="230" y="0" width="115" height="30" uuid="018825e6-aa24-4a11-85a4-760fa6cf913c">
                    <property name="com.jaspersoft.studio.spreadsheet.connectionID" value="cc7977d7-a541-4d3b-81d8-97be3451e130"/>
                    <property name="com.jaspersoft.studio.unit.width" value="px"/>
                </reportElement>
                <text><![CDATA[locCode]]></text>
            </staticText>
            <staticText>
                <reportElement x="345" y="0" width="115" height="30" uuid="16e13486-0a34-4c65-b512-00a4678c65f7">
                    <property name="com.jaspersoft.studio.spreadsheet.connectionID" value="82e0773c-3e7d-4372-a44c-93a4bfcfd6b0"/>
                </reportElement>
                <text><![CDATA[isCurrentLoc]]></text>
            </staticText>
            <staticText>
                <reportElement x="0" y="2" width="60" height="30" uuid="ad4e4685-2696-472c-a3b6-4a9961aeb6dc">
                    <property name="com.jaspersoft.studio.spreadsheet.connectionID" value="bff05bbe-0c73-4e55-adec-461fd0f2ebff"/>
                </reportElement>
                <text><![CDATA[Main Dataset]]></text>
            </staticText>
        </band>
    </columnHeader>
    <detail>
        <band height="110" splitType="Stretch">
            <textField>
                <reportElement x="115" y="0" width="115" height="30" uuid="3bffc0f6-fb80-443a-b06d-ebbe2b669388">
                    <property name="com.jaspersoft.studio.spreadsheet.connectionID" value="bff05bbe-0c73-4e55-adec-461fd0f2ebff"/>
                </reportElement>
                <textFieldExpression><![CDATA[$F{mainCallOrder}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="230" y="0" width="115" height="30" uuid="1267f406-8492-4dd1-859a-a37f24ac545a">
                    <property name="com.jaspersoft.studio.spreadsheet.connectionID" value="cc7977d7-a541-4d3b-81d8-97be3451e130"/>
                </reportElement>
                <textFieldExpression><![CDATA[$F{mainLocCode}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="345" y="0" width="115" height="30" uuid="522f3a23-a2b4-443d-9ca5-9abbd54a07df">
                    <property name="com.jaspersoft.studio.spreadsheet.connectionID" value="82e0773c-3e7d-4372-a44c-93a4bfcfd6b0"/>
                </reportElement>
                <textFieldExpression><![CDATA[$F{mainIsCurrentLoc}]]></textFieldExpression>
            </textField>
            <componentElement>
                <reportElement x="0" y="80" width="560" height="30" uuid="859af22b-90ec-41fb-acf0-36d4acf2b90a"/>
                <jr:list xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd" printOrder="Vertical">
                    <datasetRun subDataset="nextPortDataset" uuid="55c7173b-0b04-4d4f-bd80-48cfe1ef620c">
                        <datasetParameter name="MAIN_CALL_ORDER">
                            <datasetParameterExpression><![CDATA[$F{mainCallOrder}]]></datasetParameterExpression>
                        </datasetParameter>
                    </datasetRun>
                    <jr:listContents height="30" width="560">
                        <staticText>
                            <reportElement x="0" y="0" width="100" height="30" uuid="6db9b791-aaac-45e9-af0f-34546254b9e9"/>
                            <text><![CDATA[Filtering List]]></text>
                        </staticText>
                        <textField>
                            <reportElement x="115" y="0" width="115" height="30" uuid="ee03e48b-38dd-4814-ad7d-2549810c0991">
                                <property name="com.jaspersoft.studio.unit.x" value="px"/>
                            </reportElement>
                            <textFieldExpression><![CDATA[$F{callOrder}]]></textFieldExpression>
                        </textField>
                        <textField>
                            <reportElement x="230" y="0" width="115" height="30" uuid="213ab052-9cdc-419a-8d3d-45db3bf6ef90"/>
                            <textFieldExpression><![CDATA[$F{locCode}]]></textFieldExpression>
                        </textField>
                        <textField>
                            <reportElement x="345" y="0" width="115" height="30" uuid="a8ea32ea-150f-4d1f-8945-18c2b287c157"/>
                            <textFieldExpression><![CDATA[$F{isCurrentLoc}]]></textFieldExpression>
                        </textField>
                    </jr:listContents>
                </jr:list>
            </componentElement>
        </band>
    </detail>
</jasperReport>

并产生以下输出:

此示例需要使用此属性链接到主数据集和子数据集的基于文件的数据适配器:

<property name="net.sf.jasperreports.data.adapter" value="./DataAdapter.xml"/>

如果没有基于文件的数据适配器,可以创建一个,并从Jaspersoft Studio内的Repository Explorer将其导出到文件。

相关问题