In the first lessons of BIRT, you learn how to set up a sort. But the sort that you learn is static. You can set it up, but once the report runs, the user has no control over how the data sorts.
In this lesson, you will learn dynamic sorting in BIRT. That means that the user can reorganize how the data is presented simply by clicking on the column header.
This is a quick lesson on how to set it up.
First, make a copy of the Customers.rptdesign folder that you should already have ( if you are following along in the lessons). If not, you can download the file from mrbirt.com.
Rename the copied file to: Customers-dynamic-sort-BIRT.rptdesign.
Open the file.
There a several things that we need to do to make the sort dynamic.
First of all, we need to create a new report parameter (as shown).
Name: rp_SortOrder
Data Type: Integer
Display Type: Text Box
Default Value: 0
Select following Check boxes: “Is Required”
“Hidden”
Create Drill-Through Hyperlink
- Select the “Customer” label in the header of the table.
- Select “Properties” from the Property Editor.
- Select the “Hyperlink” tab
- Click the “Edit…” button
Hyperlink Options
- Select Drill-through.
- Select “BIRT-Dynamic-Sort-Customers.rptdesign”
- Select “Same Frame” for “Show Target Report”.
- Type “Sort by Customer” for a “Tool Tip”.
- Click the “Add…” button
Add Hyperlink Parameter:
- Select “rp_SortOrder” from the Parameter drop-down menu.
- Click the “Expression Builder” button next to the “Value” field.
Copy and paste the following code into the “Expression Builder” window:
// CUSTOMERNAME
if(params[“rp_SortOrder”].value == 0) { 1 } else if(params[“rp_SortOrder”].value == 1) { 2 } else { 1 }
When your screen matches the screenshot here, you’re ready to move on. Click OK.
Next, select the “Table” tab. Then, select the “Script” editor. Be sure the “Table” tab is selected or it will not work properly.
Adding the Sorting Script
- Make sure that your “Script” editor shows “Table” as the object you are working on.
- Set the Script drop-down menu to “onPrepare”
- Copy and paste the provided code into the “Script” editor.
Click Preview.
The “Customer” header is now sortable! Click it a few times.
The sort goes from ASC and back to DESC order.
You can now apply the same techniques to any of the other headers so that your report is now sortable using multiple columns.