A DataProcessor is a small library that can integrate ghtmlxGrid with the server side backend. The library monitors changes in the grid and uses a simple protocol to interchange with the server side code. The DataProcessor consists of the following two parts:
There is an add-in for dhtmlxGrid that manages the process of updating data on the server side.
Main Features:
To initialize DataProcessor and attach it to the grid, the following two commands can be used:
var dp = new dataProcessor(url); dp.init(grid);
Where:
By default, the DataProcessor will be initialized in the auto update mode which means that after each change in the grid it will send data to the server. In some cases, it makes sense to use the manual update mode:
dp.setUpdateMode("off") //... dp.sendData();
In such case, the DataProcessor will store the information about all changes in the grid, but will send them to the server only after sendData() method is executed.
There are built-in actions in dhtmlxDataProcessor that are used during data processing:
The user can define his own handlers for each of them by
DataProcessor.defineAction("update",myHandler);
In such case, myHandler will be called after update operations, returning true from custom handler will allow default update actions.
The user is able to define any other action himself. All he needs is to use the following syntax in the XML returned by the server side:
<data> <action type='[action name]'> anything </action> </data>
The following JavaScript code should also be used:
DataProcessor.defineAction("[action name]",myHandler);
myHandler function will be called and get <action> tag object as incoming argument.
The default package contains an example of the server side code for PHP (by additional request, the similar code for JSP|ColdFusion|C#.Net|RybyOnRails can be sent). The code does the following three tasks:
The incoming parameters are:
All these parameters are part of GET request.
Based on the value of !nativeeditor_status, the related section of the server side logic is triggered.
The response must be in the following format:
<data> <action type="some" sid="some" tid="some" /> </data>
Where:
The response must be a valid XML in order to be processed correctly.
To debug dataprocessor, just include both
It will provide debug console similar to next
The following data sending modes are available:
The parameters c0-cN, used by default, are not very useful on the server side. The DataProcessor allows to use grid column ids instead of them:
grid.setHeader("Name of the book,Name of the author"); grid.setColumnIds("book,author"); //... dp.enableDataNames(true);
On the server side it will look like this:
$_GET['c0'] => $_GET['book'] $_GET['c1'] => $_GET['author']
POST can be used instead of GET in the following way:
dp.setTransactionMode("POST");
By default, update for each row will be sent as a separate request. This means that when 20 rows are updated - 20 requests will be sent to the server. This is not the best approach, so instead of it a single (more complex) request can be sent to the server side:
dp.setTransactionMode("POST",true); // or dp.setTransactionMode("GET",true);
In such mode, the server side receives a slightly different set of parameters:
For example, if the user has two updated rows on the client side with ids = r2 and r3, the server side code will receive:
ids = r2,r3
The awaited server side response should be in the same format as usual, but should include data for all processed rows:
<data> <action type="some" sid="r2" tid="r2" /> <action type="some" sid="r3" tid="r3" /> </data>
The DataProcessor has the following predefined response modes:
But in some (many) cases, the user will need a way to return some additional information (the most common use-case - an error during a DB operation). In such case, an additional response type can be introduced:
dp.defineAction("error",my_action);
Where my_action is a custom function that will be called when the response of “error” type is received.
<data> <action type="error" sid="id" tid="id">Details</action> </data> <code javascript> function my_action(node){ alert(node.getAttribute("action")); // error alert(node.firstChild.data); // Details return false; }
The following common error can be enumerated:
The most probable reason: it is caused by some server side error that breaks the XML. The user can enable the debug mode and check the response of the server side to receive more information.
Actually, it is not an error as the rows will be removed only after synchronizing with the server.
The most probable reason: incorrect values of the “action” attribute in the response XML.
The most probable reason: incorrect values of the “sid” and the “tid” attributes in the response XML.
To handle a server side error with dhtmlxDataProcessor, the user can implement his own callback events (events that occur on the client side after server side's processing) on the client side:
DataProcessor.defineAction("error_123",myHandler);
On the server side, in case of an error you should return
<data> <action type="error_123"> any info </action> <data/>
In this case, myHandler function will be executed on the client side in case of an error (error_123). It will get the following as parameter(s):
Returning false from myHandler function will prevent default event processing.