如何将临时数据库内容添加到Textfield [英] How to add Ad Hoc database content to Textfield

查看:72
本文介绍了如何将临时数据库内容添加到Textfield的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个输入参数,它是一个ID.

我想使用数据库中的查找表将该ID扩展为一个长名称,以供报表标题使用.

我怎么能只通过SQL查询将文本添加到文本字段中,而不必担心表格数据的行呢?

解决方案

您可以使用 Title 区域中的 List 组件.此 List (或 Table 组件)将与另一个附加(非主)数据源关联,以显示信息(在您的情况下,通过ID传递的ID名称).

Detail 区域(或另一个具有自己数据源的 Table 组件)将使用主数据源显示按参数值(您的情况下的ID)过滤的数据

借助于 textField 的属性 isStretchWithOverflow (具有 true 值),我们可以保证所有文本都将使用 textField .

样本

在此示例中,我使用了与 Jaspersoft Studio 一起分发的数据库.参数 addrId 用于通过 address 表的 id 字段过滤数据.此参数已映射到具有相同名称( addrId )的 List 的数据源参数.是的,我使用放在 Title 波段上的 List 组件显示有关我们外部参数(此示例中的地址的城市和街道)的值的信息.在 Detail 区域中,我们显示了与我们的地址(由 addrId 定义)有关的文档(表 document )的信息

报告模板

<?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="growing_text" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="77f0cb04-7f4b-43dc-af12-89c25fa7c58c">
    <subDataset name="dsAddrTitle" uuid="0eb7cd0c-f4f1-408d-be13-dc484fda80d5">
        <property name="com.jaspersoft.studio.data.defaultdataadapter" value="Sample DB"/>
        <parameter name="addrId" class="java.lang.Integer"/>
        <queryString>
            <![CDATA[SELECT city + ', ' + street AS name FROM address WHERE id=$P{addrId}]]>
        </queryString>
        <field name="NAME" class="java.lang.String"/>
    </subDataset>
    <parameter name="addrId" class="java.lang.Integer">
        <defaultValueExpression><![CDATA[33]]></defaultValueExpression>
    </parameter>
    <queryString>
        <![CDATA[SELECT ID, ADDRESSID, TOTAL FROM DOCUMENT WHERE  ADDRESSID=$P{addrId} ORDER BY ADDRESSID]]>
    </queryString>
    <field name="ID" class="java.lang.Integer"/>
    <field name="ADDRESSID" class="java.lang.Integer"/>
    <field name="TOTAL" class="java.math.BigDecimal"/>
    <title>
        <band height="10" splitType="Stretch">
            <componentElement>
                <reportElement x="160" y="0" width="40" height="10" uuid="f4cb4e5c-e2d7-4927-b143-4cfcd7d99b76"/>
                <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" ignoreWidth="true">
                    <datasetRun subDataset="dsAddrTitle" uuid="4bf3eb57-f752-4856-ac3a-fd7e3a33f434">
                        <parametersMapExpression><![CDATA[$P{REPORT_PARAMETERS_MAP}]]></parametersMapExpression>
                        <datasetParameter name="addrId">
                            <datasetParameterExpression><![CDATA[$P{addrId}]]></datasetParameterExpression>
                        </datasetParameter>
                        <connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
                    </datasetRun>
                    <jr:listContents height="30" width="100">
                        <textField isStretchWithOverflow="true">
                            <reportElement x="0" y="0" width="40" height="10" uuid="1b533c30-7868-450b-a5b9-59d5130dcb67"/>
                            <textFieldExpression><![CDATA[$F{NAME}]]></textFieldExpression>
                        </textField>
                    </jr:listContents>
                </jr:list>
            </componentElement>
        </band>
    </title>
    <columnHeader>
        <band height="30">
            <staticText>
                <reportElement x="0" y="0" width="185" height="30" uuid="100faa3b-790d-4dc6-b86c-8911a8762207"/>
                <text><![CDATA[ID]]></text>
            </staticText>
            <staticText>
                <reportElement x="185" y="0" width="185" height="30" uuid="aef6af65-f7b5-42e9-a102-aeb272f99103"/>
                <text><![CDATA[ADDRESSID]]></text>
            </staticText>
            <staticText>
                <reportElement x="370" y="0" width="185" height="30" uuid="2c176a21-6387-4505-836e-7e250751755f"/>
                <text><![CDATA[TOTAL]]></text>
            </staticText>
        </band>
    </columnHeader>
    <detail>
        <band height="30" splitType="Stretch">
            <textField>
                <reportElement x="0" y="0" width="185" height="30" uuid="22cbe96d-5322-40e3-bd96-d2aa9bf35dd2"/>
                <textFieldExpression><![CDATA[$F{ID}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="185" y="0" width="185" height="30" uuid="3adedcc9-f60e-4664-bbe8-6b7d7b8e13a4"/>
                <textFieldExpression><![CDATA[$F{ADDRESSID}]]></textFieldExpression>
            </textField>
            <textField pattern="#,##0.00#">
                <reportElement x="370" y="0" width="185" height="30" uuid="48041fd6-1375-4819-8ebb-ffd4aef84889"/>
                <textFieldExpression><![CDATA[$F{TOTAL}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
</jasperReport>

输出结果

I have an input parameter which is an ID.

I want to use a lookup table in the database to expand that ID into a long name for use in the report title.

How could I just add text into a textfield from a SQL query without needing to worry about rows of tabular data?

解决方案

You can use the List component in the Title band. This List (or Table component) will be associated with another additional (non main) datasource for showing information (the Name by Id passed via Parameter in your case).

The main datasource will be used by Detail band (or another Table component with the its own datasource) for showing data filtered by parameter's value (Id in your case).

With help of textField's property isStretchWithOverflow (with true value) we can garantee that all text will be drawing with the textField.

The sample

In this sample I've used the DB distributed with the Jaspersoft Studio. The parameter addrId was used for filtering data by the id field of address table. This parameter was mapped to the List's datasource parameter with the same name (addrId). Yes, I've used the List component placed on Title band for showing information about the value of our external parameter (the city and the street of address in this sample). At the Detail band we are showing the information about documents (table document) related to our address (defined by addrId)

The report's template

<?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="growing_text" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="77f0cb04-7f4b-43dc-af12-89c25fa7c58c">
    <subDataset name="dsAddrTitle" uuid="0eb7cd0c-f4f1-408d-be13-dc484fda80d5">
        <property name="com.jaspersoft.studio.data.defaultdataadapter" value="Sample DB"/>
        <parameter name="addrId" class="java.lang.Integer"/>
        <queryString>
            <![CDATA[SELECT city + ', ' + street AS name FROM address WHERE id=$P{addrId}]]>
        </queryString>
        <field name="NAME" class="java.lang.String"/>
    </subDataset>
    <parameter name="addrId" class="java.lang.Integer">
        <defaultValueExpression><![CDATA[33]]></defaultValueExpression>
    </parameter>
    <queryString>
        <![CDATA[SELECT ID, ADDRESSID, TOTAL FROM DOCUMENT WHERE  ADDRESSID=$P{addrId} ORDER BY ADDRESSID]]>
    </queryString>
    <field name="ID" class="java.lang.Integer"/>
    <field name="ADDRESSID" class="java.lang.Integer"/>
    <field name="TOTAL" class="java.math.BigDecimal"/>
    <title>
        <band height="10" splitType="Stretch">
            <componentElement>
                <reportElement x="160" y="0" width="40" height="10" uuid="f4cb4e5c-e2d7-4927-b143-4cfcd7d99b76"/>
                <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" ignoreWidth="true">
                    <datasetRun subDataset="dsAddrTitle" uuid="4bf3eb57-f752-4856-ac3a-fd7e3a33f434">
                        <parametersMapExpression><![CDATA[$P{REPORT_PARAMETERS_MAP}]]></parametersMapExpression>
                        <datasetParameter name="addrId">
                            <datasetParameterExpression><![CDATA[$P{addrId}]]></datasetParameterExpression>
                        </datasetParameter>
                        <connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
                    </datasetRun>
                    <jr:listContents height="30" width="100">
                        <textField isStretchWithOverflow="true">
                            <reportElement x="0" y="0" width="40" height="10" uuid="1b533c30-7868-450b-a5b9-59d5130dcb67"/>
                            <textFieldExpression><![CDATA[$F{NAME}]]></textFieldExpression>
                        </textField>
                    </jr:listContents>
                </jr:list>
            </componentElement>
        </band>
    </title>
    <columnHeader>
        <band height="30">
            <staticText>
                <reportElement x="0" y="0" width="185" height="30" uuid="100faa3b-790d-4dc6-b86c-8911a8762207"/>
                <text><![CDATA[ID]]></text>
            </staticText>
            <staticText>
                <reportElement x="185" y="0" width="185" height="30" uuid="aef6af65-f7b5-42e9-a102-aeb272f99103"/>
                <text><![CDATA[ADDRESSID]]></text>
            </staticText>
            <staticText>
                <reportElement x="370" y="0" width="185" height="30" uuid="2c176a21-6387-4505-836e-7e250751755f"/>
                <text><![CDATA[TOTAL]]></text>
            </staticText>
        </band>
    </columnHeader>
    <detail>
        <band height="30" splitType="Stretch">
            <textField>
                <reportElement x="0" y="0" width="185" height="30" uuid="22cbe96d-5322-40e3-bd96-d2aa9bf35dd2"/>
                <textFieldExpression><![CDATA[$F{ID}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="185" y="0" width="185" height="30" uuid="3adedcc9-f60e-4664-bbe8-6b7d7b8e13a4"/>
                <textFieldExpression><![CDATA[$F{ADDRESSID}]]></textFieldExpression>
            </textField>
            <textField pattern="#,##0.00#">
                <reportElement x="370" y="0" width="185" height="30" uuid="48041fd6-1375-4819-8ebb-ffd4aef84889"/>
                <textFieldExpression><![CDATA[$F{TOTAL}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
</jasperReport>

The output result

这篇关于如何将临时数据库内容添加到Textfield的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆