Grouping by parameter value in jasper

2019-09-06 19:22发布

问题:

Well, I don't know, maybe I'm missing something, but I've been trying to group data in a jasper report, but so far the grouping appears only if the group expression is a field. Is it possible to group data by parameter value instead of field value? i.e., something like

<groupExpression><[!CDATA[$P{some_param}]]></groupExpression>

instead of

<groupExpression><[!CDATA[$F{some_field}]]></groupExpression>

in the .jrxml file?

回答1:

Is it possible to group data by parameter value instead of field value?

Yes, it is, just like you mentioned. You would have no syntax errors and the report would be generated.

The problem is that it may not bring what you're expecting. The group bands are usually printed every time the "groupExpression" changes. So basically this parameter needs to be associated with something that will change during the report generation (for example, filling a parameter of a subreport with a field in way that this subreport uses this paramater as a group expression). And of course, it needs to be associated with something that makes sense and bring you the desirable behavior.

You can have something like this in your subreport:

...
    <parameter name="START" class="java.util.Date"/>
    <parameter name="END" class="java.util.Date"/>
...

And something like this in a detail band of your "super" report:

<subreport>
     <reportElement x="0" y="10" width="555" height="200" uuid="ac2c99da-f595-4498-a518-2bfb1f31b73c"/>
         <subreportParameter name="START">
                <subreportParameterExpression><![CDATA[$F{start}]]></subreportParameterExpression>
         </subreportParameter>
         <subreportParameter name="END">
                <subreportParameterExpression><![CDATA[$F{end}]]></subreportParameterExpression>
         </subreportParameter>
         ...
</subreport>

Just notice I'm assuming the fields $F{end} and $F{start} are also "java.util.Date" objects.