Previous TopicNext Topic


Tutorial 5: Writing an event handler in JavaScript

This tutorial provides instructions for writing a set of event handlers. The tutorial assumes that you have a basic report design based on the Classic Models, Inc. Sample Database. The only requirement of the starting report design is that it contains a table of customers with a column for the customer name. In this tutorial you count the customers whose names contain the string "Mini" and display the result in a pop-up window.

In this tutorial, you perform the following tasks:

Task 1: Open the report design

Open a report design that uses the Classic Car sample database and displays a table of customer names.

  1. If necessary, open Navigator by choosing Window->Show View->Navigator.
  2. Double-click the appropriate report design.
  3. The file opens in the layout editor.

Task 2: Create and initialize a counter in the Table.onStart( ) method

In order to count the number of customers whose names contain "Mini", you must first declare a global counter and set its value to zero. You conditionally incremented this counter in the Table.onRow( ) method. The Table.onStart( ) method is the most appropriate place to do this because Table.onStart( ) executes before any rows are retrieved.

Do the following tasks:

  1. In Layout, select the table by placing the cursor near the bottom left corner of the table.
  2. The table icon appears, as in the following illustration.

  3. Select the Script tab.
  4. The script window appears, as shown in the following illustration.

  5. Type the following line of code in the script window for the onStart( ) method:
  6. var countOfMinis = 0; 
    
  7. To run the report and verify that the code did not create any errors, select Preview.
  8. Scroll to the bottom of the report, where JavaScript error messages appear. The report appears, as shown in the following illustration:

Task 3: Conditionally increment the counter in the Table.onRow( ) method

To count the number of customers with "Mini" in their name, you must examine each customer's name and add one to the counter for every occurrence. A logical place to do this is in the Table.onRow( ) method, which is executed upon every retrieval of a row of data from the data source.

  1. In Layout, select the Table and then select Code.
  2. Pull down the list of methods at the top of the script window and select onRow, as in the following illustration:
  3. The following line of JavaScript code conditionally increments the counter you created in Create and initialize a counter in the Table.onStart( ) method.
  4. if(row["customerName"].indexOf("Mini") != -1) countOfMinis += 1; 
    
    You can type this line of code directly, or you can take advantage of a palette of JavaScript objects, classes, and operators. When you select the Code tab, BIRT changes Palette from a collection of report items to a tree of JavaScript objects and operators. Palette contains the same selection of objects, classes, and operators as Expression Builder.
    You can use the JavaScript palette for each of the following elements:
    • row["customerName"]
    • Select This Report's Data Sets->Customers->CUSTOMERNAME.
    • indexOf( )
    • Select Native (JavaScript) Objects->String Functions->indexOf( )
    • !=
    • Select Operators->Comparison->!=
    • +=
    • Select Operators->Assignment->+=
    In this tutorial, you type in the entire line.
  5. Select Preview to run the report again to verify that the code you entered did not create any errors. If you see an error message, you may have mis-typed the statement. If so, go back to the script window, select the method you just modified, correct the error, and try it again.

Task 4: Display the result, using the Table.onFinish( ) method

To display the count of customers with "Mini" in their name, you must insert code in a method that runs at the completion of processing the table. The place to do this is in the Table.onFinish( ) method.

  1. Select the onFinish( ) method from the Table's script window pull-down menu.
  2. Type the following code into the onFinish( ) method:
  3. importPackage(Packages.javax.swing); 
    frame = new JFrame("Count of Minis = " + countOfMinis); 
    frame.setBounds(310, 220, 300, 20); 
    frame.show(); 
    
  4. Select Preview to see the results. If there were no errors in the code, you see a report similar to the one in the following illustration.
  5. If you do not see the Count of Minis window, look for it behind the Eclipse window. If the Count of Minis window does not appear, the most likely reason is a scripting error caused by an error in one of your code entries. If you suspect that a scripting error has occurred, scroll to the bottom of the report, where all scripting error messages appear. In most situations, there is a brief error message next to a plus sign (+). The plus sign indicates that there is a more detailed error message that is only visible after you expand the brief error message. To expand the brief error message, select the plus sign. Scroll down to see the more detailed error message.

(c) Copyright Actuate Corporation 2006

Previous TopicNext Topic