Jasper使用comparatorExpression报告交叉表排序 [英] Jasper Reports crosstab sorting with comparatorExpression

查看:169
本文介绍了Jasper使用comparatorExpression报告交叉表排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据一些自定义方案在交叉表中对动态列进行排序。



在文档中我发现了 comparatorExpression:Crosstab group bucket comparator表达式。此表达式的结果是
,用于按升序或降序对存储桶进行排序。如果没有指定比较器
表达式,将使用自然顺序。



但我不明白表达式应该是什么样子。我可以以某种方式使用常规的Java比较器吗?有人可以分享一个例子吗?

解决方案

我遇到了同样的问题,没有找到任何例子或解释,但我发现了去做吧。我会解释,所以希望其他人可以使用这个解决方案。



我使用了下面的代码来表示jasperreports 3.1.0和iReport 3.1.4,但我猜它适用于几乎所有版本。



首先,您必须确保知道您的行/列组的存储桶表达式中的类。默认情况下这是java.lang.String,但我有一个自定义类。为此,我需要为我的列组编辑这样的xml:

 < bucketExpression class =java.lang .String><![CDATA [$ F {customObj}]]>< / bucketExpression> 

 < bucketExpression class =com.project.CustomObj><![CDATA [$ F {customObj}]]>< / bucketExpression> 

显然,这个customObj值是一个具有相应类的字段,在报告本身中定义。



然后你需要添加一个Comparator作为参数,例如:

  parameters.put(OVERRIDE_Comparator,new Comparator< CustomObj>(){
public int compare(CustomObj c1,CustomObj c2){
//在这里创建自定义比较逻辑,此代码有效好像没有使用自定义Comparator
返回c1.compareTo(c2);
}
});

现在使用java.util.Comparator参数类在jasperreport中添加这样的OVERRIDE_Comparator参数。 / p>

最后一步:将$ P {OVERRIDE_Comparator}作为比较器表达式放在您需要的行/列组中。



编译此类报告时,最可能的编译错误是转换问题。 Jasperreports默认为java.lang.String,您可能需要手动编辑报表的xml以在每一步获得正确的类。



(我发现这个方法从一些亚洲网站,谢天谢地,代码本身是可读的!:-))


I'm trying to sort my dynamic columns in a cross tab according to some custom scheme.

In the docs I found mention of comparatorExpression: Crosstab group bucket comparator expression. The result of this expression is used to sort the buckets, in ascending or descending order. If no comparator expression is specified, the natural order will be used.

but I don't understand what the expression should look like. Can I somehow use a regular java comparator? Can someone share an example?

解决方案

I've had the same issue and did not find any examples or explanation but I found how to do it. I'll explain, so hopefully other people can use this solution.

I've used the code below for jasperreports 3.1.0 and iReport 3.1.4, but I guess it works for pretty much all versions.

First you'll have to make sure that you know wich class you have in the bucket expression for your row/column group. By default this is java.lang.String, but I have a custom class there. For this to work I needed to edit the xml like this for my column group:

<bucketExpression class="java.lang.String"><![CDATA[$F{customObj}]]></bucketExpression>

to

<bucketExpression class="com.project.CustomObj"><![CDATA[$F{customObj}]]></bucketExpression>

Obviously this customObj value is a field with the corresponding class, defined in the report itself.

Then you'll need to add a Comparator as a parameter, for example:

parameters.put("OVERRIDE_Comparator", new Comparator<CustomObj>() {
    public int compare(CustomObj c1, CustomObj c2) {
        //create your custom compare logic over here, this code works as if no custom Comparator is used
        return c1.compareTo(c2);
    }
});

Now add such a OVERRIDE_Comparator parameter in the jasperreport, using the java.util.Comparator Parameter Class.

Final step: Put $P{OVERRIDE_Comparator} as the Comparator Expression in the row/column group you needed.

When compiling such a report, the most likely compile error would be casting issues. Jasperreports defaults to java.lang.String, you might need to edit the xml of the report manually to get the correct class at each step.

(I found out this method from some asian site, thankfully the code itself was readable! :-) )

这篇关于Jasper使用comparatorExpression报告交叉表排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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