Jasper报告最后排序null/empty值

Per*_*los 1 jasper-reports

我有报告组和子组,需要按字母顺序排序,但我想要排序null或空值作为最后.是否有选择或一些工作来制作它?谢谢帕维尔

Ale*_*x K 9

您可以通过排序选项和其他变量来实现此结果.

步骤:

<variable name="isNull" class="java.lang.Integer">
    <variableExpression><![CDATA[Strings.isNullOrEmpty($F{fieldName})]]></variableExpression>
</variable>
Run Code Online (Sandbox Code Playgroud)
  • 添加两个排序指令 - 按新变量排序(首先排序)和按字段排序(排在第二位)
<sortField name="isNull" order="Ascending" type="Variable"/>
<sortField name="fieldName"/>
Run Code Online (Sandbox Code Playgroud)

完整的工作样本.该JRXML文件:

<?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>
Run Code Online (Sandbox Code Playgroud)

用于测试的Java代码:

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);
}
Run Code Online (Sandbox Code Playgroud)

在此示例中,Address是一个简单的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;
    }
}
Run Code Online (Sandbox Code Playgroud)

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

生成的报告采用*PDF*格式

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


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

<sortField name="isNull" order="Descending" type="Variable"/>
Run Code Online (Sandbox Code Playgroud)

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