Pergunta

In my report there are currently 2 subreports, let's call them A and B.
The A lists Cash positions, and the B lists Stocks.
I run this report from a web based Java environment, the JasperReports's report reads from a sql database.

I want to pass an argument to the JR report that tells it in what order to arrange the subreports, in this case for e.g. (B first, then A or vice versa).

Is there a way to accomplish this?

Foi útil?

Solução

You should send a parameter of boolean type. while generating report. on the basis of that parameter you should define the path of subreport. Here is jrxml code for subrepor path.e.g.

For first subreport:

<subreport>
            <reportElement uuid="8dba7f58-0466-4504-9d51-7484786450d2" positionType="Float" x="0" y="16" width="315" height="16"/>              
            <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{listOfObject})]]></dataSourceExpression>
            <subreportExpression><![CDATA[$P{swap} == true ? "/path/to/first/subreport" : /path/to/second/subreport]]></subreportExpression>
        </subreport>

And for second subreport:

<subreport>
        <reportElement uuid="8dba7f58-0466-4504-9d51-7484786450d2" positionType="Float" x="0" y="16" width="315" height="16"/>              
        <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{listOfObject})]]></dataSourceExpression>
        <subreportExpression><![CDATA[$P{swap} == true ? "/path/to/second/subreport" : /path/to/first/subreport]]></subreportExpression>
    </subreport>

And vice-versa in other case.I have not tested. Please have a look.

Enjoy.

Outras dicas

You could use a more complex subreportExpression and "Print When Expression". In first subreport set print when to something like

$P{NUM_OF_SUBS} <= 1 ? true : false

in second

$P{NUM_OF_SUBS} <= 2 ? true : false

etc... And for subreportExpression in first subreport something like:

$P{SUBS}.split(",")[1] == "A" 
    ? "repo:subA.jrxml"
    : $P{SUBS}.split(",")[1] == "B"
      ? "repo:subB.jrxml"
      : $P{SUBS}.split(",")[1] == "C"
        ? "repo:subC.jrxml"
        : "repo:subD.jrxml"

and in second:

$P{SUBS}.split(",")[2] == "A" 
    ? "repo:subA.jrxml"
    : $P{SUBS}.split(",")[2] == "B"
      ? "repo:subB.jrxml"
      : $P{SUBS}.split(",")[2] == "C"
        ? "repo:subC.jrxml"
        : "repo:subD.jrxml"

etc...

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top