Previous TopicNext Topic


Tutorial 7: 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: Define a data source and a data set

Define a data souce and data set using the Classic Car sample database.

  1. Right click Data Sources in the Data Explorer View and choose Classic Models Inc. Sample Database from the list of available data sources.
  2. Right click Data Sets and choose the data source you created in step 1.
  3. Expand CLASSICMODELS to show the tables in the data source.
  4. Expand CUSTOMERS to show the columns of the CUSTOMERS data set.
  5. Drag CUSTOMERNAME from CUSTOMERS onto the Select field of the query editor.
  6. Drag CUSTOMERS from CLASSICMODELS onto the From field of the query editor.
  7. Choose Finsish in the Query editor.
  8. Choose OK in the Output Column editor.

Task 2: Create a simple report design

Create a report layout using the data set you defined in Task 1: "Define a data source and a data set".

  1. Select the Layout tab.
  2. Drag the List icon from the Palette View and drop it in the Layout editor.
  3. Drag CUSTOMERNAME from the Data Explorer view and drop it on the Detail row of the list in the Layout editor. The layout editor appears as shown in Figure 23-17.
  4. Figure 34-4 Report design in the layout editor

    Figure 23-17 Report design in the layout editor

Task 3: Create and initialize a counter in the List.onCreate( ) method

In order to count the number of customers whose names contain the string "Mini," you must first set a persistent global variable to zero. The List.onCreate( ) method is the most appropriate place to do this task because List.onCreate( ) executes before any rows are retrieved. You conditionally increment this counter in the Row.onCreate( ) method.

  1. In Layout, select the list by placing the cursor near the bottom-left corner of the list. The list icon appears, as shown in Figure 23-18.
  2. Figure 24-5 Table icon in the layout editor

    Figure 23-18 List icon in the layout editor
  3. Choose the Script tab. The script editor appears, as shown in Figure 23-19.
  4. Figure 24-6 Script window

    Figure 23-19 Script editor
  5. Choose onCreate from the pull-down list of event handlers, as shown in Figure 23-20.
  6. Figure 23-20 Choose the onCreate event handler
  7. Type the following line of code in the script editor for the onCreate( ) method:
reportContext.setPersistentGlobalVariable("cmKey", 
new java.lang.Integer("0")); 
  1. To run the report and verify that the code did not create any errors, choose Preview.
  2. Scroll to the bottom of the report, where JavaScript error messages appear. If there are no errors, the report appears, as shown in Figure 23-21.
  3. If you see an error message, you may have typed a statement incorrectly. If so, go back to the script window, select the method you just modified, correct the error, and choose Preview again.

    Figure 34-7 Report preview

    Figure 23-21 Report preview

Task 4: Conditionally increment the counter in the Detail.onCreate( ) method

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

  1. In Outline View, expand Body, List, and Detail, and choose Data[CUSTOMERNAME], as shown in Figure 23-22.
  2. Figure 23-22 Choose element in Outline View
  3. Choose the Script tab.
  4. Choose onCreate from the pull-down list of methods.
  5. Enter the following line of JavaScript code in the script editor:
  6. row_data = this.getRowData(); 
    
    Notice that when you enter the period after this, a pop-up appears containing all the available methods and properties, including getRowData. This line of code gets an instance of IRowData. You use the method, getExpressionValue( ), on IRowData to get the contents of a column of the row.
  7. Type the following line of JavaScript below the line you just entered:
  8. CustName=row_data.getExpressionValue( "row[CUSTOMERNAME]" ); 
    
    This line of code returns the contents of the list column that comes from the CUSTOMERNAME column in the data set.
  9. Type the following lines of code to conditionally increment the persistent global variable you created in Create and initialize a counter in the List.onCreate( ) method.
  10. if( CustName.indexOf( "Mini" ) != -1 ){ 
      cnt = reportContext.getPersistentGlobalVariable("cmKey"); 
      reportContext.setPersistentGlobalVariable("cmKey", 
        new java.lang.Integer(cnt.intValue() + 1 ) ); 
      } 
    
    You can use the JavaScript palette to insert each of the following elements in the preceding line:
    • indexOf( )
    • Select Native ( JavaScript ) Objects->String Functions->indexOf( )
    • != and +=
    • Select Operators->Comparison->!= and Operators->Assignment->+=
  11. Choose Preview to run the report again to verify that the code you entered did not create any errors.

Task 5: Display the result, using the
ReportDesign.afterFactory( ) method

To display the count of customers with the string "Mini" in their names, you insert code in a method that runs after the processing of all the rows in the list. One logical place for this code is in the ReportDesign.afterFactory( ) method.

  1. In Outline, select the report design.
  2. Select the afterFactory( ) method from the script window drop-down list.
  3. Type the following code into the afterFactory( ) method:
  4.   importPackage( Packages.javax.swing ); 
      countOfMinis = reportContext.getPersistentGlobalVariable(
        "cmKey").intValue(); 
      frame = new JFrame( "Count of Minis = " + countOfMinis ); 
      frame.setBounds( 310, 220, 300, 20 ); 
      frame.show( ); 
    
  5. Select Preview to see the results. If there were no errors in the code, you see a report similar to the one in Figure 23-23.
  6. Figure 24-10 Result of changing the afterFactory( ) method

    Figure 23-23 Result of changing the afterFactory( ) method

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 you can view. To expand the brief error message, choose the plus sign. Scroll down to see the more detailed error message.


(c) Copyright Actuate Corporation 2006

Previous TopicNext Topic