Previous Next

Using JavaScript to Write an Event Handler : Tutorial 7: Writing an event handler in JavaScript : Task 3: Conditionally increment the counter in the Row.onCreate( ) method

Task 3:

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 perform this task is in the Row.onCreate( ) method, which executes with every retrieval of a row of data from the data source.

1
2

Figure 23-8 onCreate( ) in the Script window

Figure 23-8
onCreate( ) in the script window
3
countOfMinis = reportContext.getPersistentGlobalVariable("cmKey");
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, which has a method, getExpressionValue( ), to get the contents of a column of the row.
4
CustName=row_Data.getExpressionValue( "row[CUSTOMERNAME]" );
This line of code returns the contents of the table column that comes from the CUSTOMERNAME column in the data set.
5
if(CustName.indexOf("Mini") != -1){
mCount = countOfMinis.intValue();
mCount += 1;
countOfMinis = new Packages.java.lang.Integer(mCount);
reportContext.setPersistentGlobalVariable("cmKey", countOfMinis);
}
You can use the JavaScript palette to insert each of the following elements in the preceding lines:
n
Select Native ( JavaScript ) Objects->String Functions->indexOf( )
n
Select Operators->Comparison->!=
n
Select Operators->Assignment->+=
6

(c) Copyright Actuate Corporation 2008