0

I have a simple report with two fields: ID and NAME. I want to sort the detail by ID or NAME depending on a parameter selected by the user in runtime.

I've tried doing something like this:

<sortField name="sort" type="Variable"/>

<variable name="sort" class="java.lang.String">
    <variableExpression>
        <![CDATA[$P{ord}.equals("name") ? $F{entity}.getName() : $F{entity}.getId().toString()]]>
    </variableExpression>
</variable>

EDIT 1: I'm passing the datasource as a parameter, I don't execute the sql query from JasperReports. I think I could pass the datasource alredy sorted to JasperReports, but i'd like to sort the data directly from the report.

EDIT 2: I reviewed the previous code and works fine for me.

4

2 回答 2

0

只需使用默认值“ID”创建一个参数“Sort_order”并在查询中添加 order by 子句。

  SELECT...
  FROM..
  ORDER BY $P!{Sort_order} DESC
于 2013-04-16T06:36:55.947 回答
0

您可以在 SELECT 中使用 ORDER BY 子句。但是,有时由于内部化或类似的事情,您需要按 jasper 对数据进行排序。解决此问题的一种方法是选择特殊列进行排序(POSTGRESQL):

SELECT 
    id, name,
    (CASE WHEN $P!{Sort_order}='id' THEN id ELSE null END) sort_id,
    (CASE WHEN $P!{Sort_order}='name' THEN name ELSE null END) sort_name
FROM
    your_table;

然后你设置按列排序sort_namesort_id

于 2015-02-26T12:16:49.487 回答