KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Registering a Javascript function as event handler for Data Grid
PRODUCT: 4D Web 2.0 Pack | VERSION: 11.1 | PLATFORM: Mac & Win
Published On: July 8, 2008

PLEASE NOTE: This Tech Tip applies to the 4D Ajax Framework v11 Release 1

The Data Grid object in the 4D Ajax Framework has some custom Javascript events you can take advantage of in your Javascript code. There are a couple of different ways to register a function to handle a particular event: Declare an anonymous function inline; or assign a reference to the function, and declare it separately.

Using the onDataColumnClick event as an example, the anonymous function would be declared like this:

var myGrid = new dax_dataGrid('Table1', null);
myGrid.go();

myGrid.onDataColumnClick = function(column, fieldReference) {
    //Some code to respond to onDataColumnClick event
};


Note there is a semi-colon at the end because this is not just a function declaration; it is a statement.

And the separately declared function would look like this:

var myGrid = new dax_dataGrid('Table1', null);
myGrid.go();

myGrid.onDataColumnClick = doSomethingWithColumn;

function doSomethingWithColumn(column, fieldReference) {
    //Some code to respond to onDataColumnClick event
}


Note the lack of parentheses and parameters in assigning the function to the event handler. A function is an object in Javascript. A function call is not being made here, but rather a reference to the function object is being assigned. If parentheses are included, the function will simply try to execute when the code is loaded rather than register as the event handler, and will not work as expected.

For the latest information about the 4D Ajax Framework please see the Daxipedia:

http://daxipedia.4d.com