JasperReport:如何在表格中显示动态列表

时间:2015-05-18 08:47:30

标签: jasper-reports

我想在表格中显示列表。列表查询的参数基于表项的值。例如:

<subDataset name="dsLines" uuid="a47307ff-90a8-476f-afd1-0fd8aa0517d0">
    <parameter name="formalId" class="java.lang.String"/>
    <queryString language="SQL">
    <![CDATA[SELECT s.formalid, sl.qty, sl.subtotal, sl.currency,
                    sl.product_localsku, sl.product_name,
                    sl.product_price,
                    sl.finalprice,
                    sl.promoids 
                    FROM salesorder s LEFT JOIN salesorderline sl
                    ON (s.id = sl.salesorder_id)
                    WHERE s.formalid = $P{formalId}
                    ORDER BY sl.product_localsku]]>
            </queryString>
    <field name="qty" class="java.lang.Integer"/>
    <field name="subtotal" class="java.math.BigDecimal"/>
    <field name="product_localsku" class="java.lang.String"/>
    <field name="product_name" class="java.lang.String"/>
    <field name="product_price" class="java.math.BigDecimal"/>
    <field name="finalprice" class="java.math.BigDecimal"/>
    <field name="promoids" class="java.util.List"/>
</subDataset>

我想要做的是创建一个基于promoids的数据集查询。我创建了一个这样的查询:

<subDataset name="promoDiscountDs" uuid="f276a182-41da-432e-b611-b5f47bfaed9a">
        <property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
        <queryString language="SQL">
            <![CDATA[select name as discount_name,
                     discount_percentage, 
                     discount_amount from promo
                     where id = ANY($F{promoids})]]>
        </queryString>
        <field name="discount_name" class="java.lang.String"/>
        <field name="discount_amount" class="java.lang.String"/>
        <field name="discount_percentage" class="java.lang.String"/>
    </subDataset>

但是,这不起作用。当我运行报告时,将显示如下的查询错误:

org.postgresql.util.PSQLException: ERROR: syntax error at or near "$"
  Position: 94
    org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2198)
    org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1927)
    org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:255)
    org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:561)
    org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:419)
    org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc2Statement.java:304)
    com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeQuery(NewProxyPreparedStatement.java:404)
    net.sf.jasperreports.engine.query.JRJdbcQueryExecuter.createDatasource(JRJdbcQueryExecuter.java:233)
    net.sf.jasperreports.engine.fill.JRFillDataset.createQueryDatasource(JRFillDataset.java:1087)
    net.sf.jasperreports.engine.fill.JRFillDataset.initDatasource(JRFillDataset.java:668)
    net.sf.jasperreports.components.list.FillDatasetRun.start(FillDatasetRun.java:162)
    net.sf.jasperreports.components.list.VerticalFillList.prepare(VerticalFillList.java:93)
    net.sf.jasperreports.engine.fill.JRFillComponentElement.prepare(JRFillComponentElement.java:149)
    net.sf.jasperreports.engine.fill.JRFillElementContainer.prepareElements(JRFillElementContainer.java:331)
    net.sf.jasperreports.engine.fill.JRFillFrame.prepare(JRFillFrame.java:217)
    net.sf.jasperreports.engine.fill.JRFillElementContainer.prepareElements(JRFillElementContainer.java:331)
    net.sf.jasperreports.engine.fill.JRFillBand.fill(JRFillBand.java:384)
    net.sf.jasperreports.engine.fill.JRFillBand.fill(JRFillBand.java:358)
    net.sf.jasperreports.engine.fill.JRVerticalFiller.fillColumnBand(JRVerticalFiller.java:2046)
    net.sf.jasperreports.engine.fill.JRVerticalFiller.fillDetail(JRVerticalFiller.java:778)
    net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReportStart(JRVerticalFiller.java:288)
    net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReport(JRVerticalFiller.java:151)
    net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:909)
    net.sf.jasperreports.engine.fill.JRFillSubreport.fillSubreport(JRFillSubreport.java:659)
    net.sf.jasperreports.engine.fill.JRSubreportRunnable.run(JRSubreportRunnable.java:59)
    net.sf.jasperreports.engine.fill.AbstractThreadSubreportRunner.run(AbstractThreadSubreportRunner.java:203)
    java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    java.lang.Thread.run(Thread.java:745)

有没有人知道如何做到这一点?

1 个答案:

答案 0 :(得分:1)

基本认为你必须注意到你在查询中使用了Field $ F {promoids},这在当前上下文中不存在。 接下来你需要了解的是$ F {}字段是数据集查询的结果。 最后一件事是你只能在查询中使用参数$ P {}。 如果您的列表组件基于表中的字段$F{promoids},则在数据集promoDiscountDs中创建一个参数,例如$P{promoids},最后将字段$F{promoids}映射到参数$P{promoids},然后在数据集promoDiscountDs的查询中使用该参数,如下所示

where id = ANY($P{promoids})

希望这可以解决你的问题。