JRXML可以在使用之前扩展数据源吗? [英] Can JRXML extend data source before using it?

查看:108
本文介绍了JRXML可以在使用之前扩展数据源吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用相同的数据源生成多个报告。其中一个报告需要在底部打印一些空行(保留),以便用户可以手动手动写入缺失数据。

I use the same data source for generating several reports. One of the reports needs to be printed with a few empty lines ("reserve") at the bottom, so that users can manually write missing data by hand if needed.

例如:

+---------+-------------+
| item 1  | bla bla     |
+---------+-------------+
| item 2  | foo         |
+---------+-------------+
|         |             |   <--- here user can just add forgotten
+---------+-------------+        items with a pen
|         |             |
+---------+-------------+

如果在数据源末尾有几条 null 的记录,则JRXML最简单。然后它只会根据需要打印其细节乐队几次没有文字。但是,数据源会被重用于其他报告,这些报告当然不希望这样。

The easiest for the JRXML would be if there were several records of nulls at the end of the data source. Then it would just print its "details" band a few times with no text, just as required. However, the data source is reused for other reports which certainly don't want this.

我可以以某种方式将这些空行注入到JRXML本身的数据源中报告填写?

Can I somehow inject such empty lines into the data source in JRXML itself, just before report filling?

推荐答案

您可以修改CustomDataSource并从jrxml设置它以在需要时生成额外记录:

You can modify your CustomDataSource and set it from jrxml to generate extra records when needed:

示例

public class JRExtraEmptyRecordsDataSource extends JRBeanCollectionDataSource {

    private int nrOfEmptyRecords=0;
    private int currentExtraRecord = 0;

    public JRExtraEmptyRecordsDataSource(Collection<?> beanCollection) {
        super(beanCollection);
    }       

    @Override
    public Object getFieldValue(JRField field) throws JRException {
        if (currentExtraRecord==0){
            return super.getFieldValue(field);
        }
        Class<?> theCorrectClass = field.getValueClass();
        //Implement your logic to return correct class (reflection or switch) if you need speciall values
        //or just return null
        return null;
    }

    @Override
    public boolean next() {
        boolean next = super.next();
        if (next){
            return true;
        }
        currentExtraRecord++;
        return currentExtraRecord<=nrOfEmptyRecords;
    }

    @Override
    public void moveFirst() {
        super.moveFirst();
        currentExtraRecord=0;
    }


    public int getNrOfEmptyRecords() {
        return nrOfEmptyRecords;
    }

    public int setNrOfEmptyRecords(int nrOfEmptyRecords) {
        this.nrOfEmptyRecords = nrOfEmptyRecords;
        return this.nrOfEmptyRecords;//Lets return something for variable
    }

}

当您需要额外的空记录时,在您的jrxml中添加此变量定义

When you need extra empty records add this variable definition in your jrxml

<variable name="extraRecords" class="java.lang.Integer">
    <initialValueExpression><![CDATA[((my.package.JRExtraEmptyRecordsDataSource)$P{REPORT_DATA_SOURCE}).setNrOfEmptyRecords(2)]]></initialValueExpression>
</variable>



我没有自定义数据源我该怎么办?



通常没有自定义数据源,您可以使用 columnFooter ,其属性为 isFloatColumnFooter =true(或虚拟组页脚带)在详细信息区域末尾显示额外信息。可能我会选择这种方法,因为它有助于自定义额外的行,并避免在正常的详细信息中检查 null band

I do not have custom datasource what can I do?

Normally without custom datasource, you can use the columnFooter with the attribute isFloatColumnFooter="true" (or a dummy group footer band) to display extra info at the end of your detail band. Probably I would choose this method anyway since it helps to customize the extra rows and avoid checking for null in the normal detail band

如果需要动态指示空记录的数量,请包含一个空记录的子报表,并作为数据源传递 JREmptyDataSource

If you need dynamically to indicate the number of empty records, include a subreport with an empty record and pass as datasource JREmptyDataSource

new net.sf.jasperreports.engine.JREmptyDataSource(P{nrOfEmptyRecords})

这篇关于JRXML可以在使用之前扩展数据源吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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