Jasper报表最后排序空值/空值 [英] Jasper report sort null/empty values last

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

问题描述

我在报告组和子组中,需要按字母顺序对它们进行排序,但我想最后对空值或空值进行排序.是否有选择或一些解决方法可以实现?谢谢帕维尔

i have in report group and subgroup and need to sort them alphabetically but i want sort null or empty values as last. Is there option or some work around to made it? Thanks Pavel

推荐答案

您可以借助排序选项和其他变量来获得此结果.

You can achieve this result with help of sorting options and additional variable.

步骤:

  • 添加变量以确定该字段为空还是空.例如,在 guava库的帮助下,此变量的表达式可以像这样(参见
  • Add the variable for determine if the field is empty or null. For example, with help of guava library the expression for this variable can be like this (see Strings.isNullOrEmpty(String) method)
<variable name="isNull" class="java.lang.Integer">
    <variableExpression><![CDATA[Strings.isNullOrEmpty($F{fieldName})]]></variableExpression>
</variable>

  • 添加两个排序说明-用于按新变量排序(排在第一位)和用于字段排序(排在第二位)
  • <sortField name="isNull" order="Ascending" type="Variable"/>
    <sortField name="fieldName"/>
    


    完整的工作示例. jrxml 文件:


    The full working sample. The jrxml file:

    <?xml version="1.0" encoding="UTF-8"?>
    <jasperReport ...>
        <import value="com.google.common.base.*"/>
        <field name="id" class="java.lang.Integer"/>
        <field name="city" class="java.lang.String"/>
        <sortField name="isNull" order="Ascending" type="Variable"/>
        <sortField name="city"/>
    <variable name="isNull" class="java.lang.Integer">
        <variableExpression><![CDATA[Strings.isNullOrEmpty($F{city})]]></variableExpression>
    </variable>
        <detail>
            <band height="20" splitType="Stretch">
                <textField>
                    <reportElement x="0" y="0" width="100" height="20"/>
                    <textElement/>
                    <textFieldExpression><![CDATA[$F{id}]]></textFieldExpression>
                </textField>
                <textField>
                    <reportElement x="100" y="0" width="100" height="20"/>
                    <textElement/>
                    <textFieldExpression><![CDATA[$F{city}]]></textFieldExpression>
                </textField>
            </band>
        </detail>
    </jasperReport>
    

    用于测试的Java代码:

    The Java code for testing:

    private void testReport() throws JRException {
        JasperReport jasperReport = JasperCompileManager.compileReport(reportSource);
        JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, getDataSource());
    
        JasperExportManager.exportReportToPdfFile(jasperPrint, outputFileName);
    }
    
    private JRDataSource getDataSource() {
        Collection<Address> list = new ArrayList<Address>();
    
        list.add(new Address(1, "Dallas"));
    
        list.add(new Address(2, "Dallas"));
    
        list.add(new Address(3, "Dallas"));
    
        list.add(new Address(5, null));
    
        list.add(new Address(4, ""));
    
        list.add(new Address(6, "Boston"));
    
        list.add(new Address(7, "Chicago"));
    
        list.add(new Address(8, "Dallas"));
    
        list.add(new Address(9, ""));
    
        list.add(new Address(10, null));
    
        list.add(new Address(11, null));
    
        return new JRBeanCollectionDataSource(list);
    }
    

    在此示例中, 地址 是简单的 POJO :

    public class Address {
    
        private Integer m_id;
        private String m_city;
    
        public Address(Integer id, String city) {
            m_city = city;
            m_id = id;
        }
    
        public Integer getId() {
            return m_id;
        }
    
        public String getCity() {
            return m_city;
        }
    }
    

    结果将是(生成的 PDF 文件):

    The result will be (generated PDF file):

    在示例中,我使用了基于 Java bean 的数据源,但是该方法也适用于 jdbc 数据源.

    In the sample I've used a Java bean based datasource, but this method is also works for jdbc datasource.

    您可以通过替换排序选项来轻松进行反向排序(将 order 属性的值从升序更改为降序 >):

    You can easy reverse sorting by replacing sort option (change the value of order property from Ascending to Descending):

    <sortField name="isNull" order="Descending" type="Variable"/>
    

    此表达式将对数据进行排序,以在列表顶部显示空值和空值.

    this expression will sort data for showing null and empty values in the top of list.

    这篇关于Jasper报表最后排序空值/空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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