Handle Unconventional Grouping

Question

I am new to JasperReport. I am working on a report which shows transaction data in a table. I have several groups in the table (the group number is not fixed), for the 1st item of each group, I don’t need to show the “+” sign before the item’s actual text. If there are more than one item in a group, then I need to show the “+” sign before each item’s actual text and calculate subtotal. If there is only one item in a group, I don’t need to calculate the subtotal.

In the table, I only need to show items for each group, and don’t need to show the group name. Is there any idea about how to show the “+” sign conditionally and how to calculate the subtotal conditionally?

 

(for group1)

item1 value1

+item2 value2

subtotal group1

(for group2)

item1 value1

+item2 value2

+item3 value3

subtotal group2

(for group3)

item1 value

 

Answer

Your problem can be solved by hiding certain data. You can prepare data format JasperReport requires and present data with table controls. You can handle the preparation in JasperReport, SQL, or a stored procedure. Here I hack it in SPL:

A

B

C

1

=myDB1.query(“select   Seller,Client,Amount from sOrder where Amount>?”,arg)

2

=create(item,value)

3

for A1.group(SellerId)

>A2.insert(0:A3,if(#>1,“+”)+Client,Amount)  

4

if A3.len()>1

>A2.insert(0,A3.SellerId+“subtotal:”,A3.sum(Amount))  

5

result A2

A1: Retrieve data from the database; arg is the parameter.

A2: Create a table sequence of two fields.

A3-B3: Group data and insert it into the table sequence row by row; precede a row with “+” sign if there is more than one item in a group.

B2-B3: Add a row of subtotal if there is more than one row in a group.

A5: Return A2’s table sequence.

You can connect to esProc from JasperReport via JDBC. The method of calling an SPL script is the same as that of calling a stored procedure. For more details, refer to How to Call an SPL Script in JasperReport