Initializing DataProcessor

Object constructor

To create a dataProcessor object, use the dataProcessor constructor:

var dp = new dataProcessor("php/update.php"); //"php/update.php" - url to the data feed

Attaching dataProcessor object to a data component

To attach a dataProcessor to a data component, use the init() method:

mygrid = new dhtmlXGridObject('gridbox');  mygrid.setSkin("dhx_skyblue");                
mygrid.setHeader("Book Title, Author, Sales");  
var dp = new dataProcessor("php/update.php"); dp.init(mygrid); // mygrid - object of the component dataProcessor will be attached to

Related sample:  Basic init of dataprocessor

dataProcessor is initialized in the auto-update mode by default, i.e. after each change in the grid, data is sent to the server.

In some cases it makes sense to disable the auto-update mode and send data to the server manually:

dp.sendData(); //sends data of the attached component to the server

Related sample:   Sending all at once

So now dataProcessor stores the information about all the changes made in the grid and sends them to the server only after the sendData() method was called.

Using dataProcessor with dhtmlxConnector

If you use dhtmlxConnector (php, java, .net) you can take its connector file as a parameter of the constructor.

myDP = new dataProcessor("myconnector.php");

Note, the client side encodes data as UTF-8, and if your server doesn't use it as the default encoding, it may process the incoming data as a differently encoded one, which may lead to problems.

Using dataProcessor with REST API

Since version 4.1 of DHTMLX library dataProcessor can be used not only with dhtmlxConnector, but also with server-side REST API. The obvious advantage of this technology is that it's used by various frameworks (PHP,.NET an so on).

To set the REST mode, use the setTransactionMode function with the REST parameter (upper case is a must). Pay attention, that you should set the REST mode only after the initialization of dataProcessor.

myDataProcessor = new dataProcessor("php/json.php"); //lock feed url
myDataProcessor.init(mygrid); //link dataprocessor to the grid

After that all requests to the server will be processed according to the rules of the REST protocol.

JSON responses

REST API returns a JSON object, thus giving dataProcessor the ability to work with JSON responses.

So instead of the long XML response

   <action type="some" sid="some" tid="some" />

You get a short one in JSON format:

//for example

A more complex example can look as:

{"action":"updated", "sid":15, "tid":15}

If you need to return an error, the response should be as follows:

{"action":"error", ...}

You can pass any data in a JSON response. Besides, the response can be received on the client side by means of the onAfterUpdate event.

Using dataProcessor in JSON mode

Since version 5.1 of DHTMLX library dataProcessor can be used with JSON data sending mode.
In this mode the client side sends a serialized object as a payload of data saving request.

myDataProcessor = new dataProcessor("php/json.php"); //lock feed url
myDataProcessor.init(mygrid); //link dataprocessor to the grid

On the server side, you can read the body of request and deserialize it like this:

$pack = json_decode(file_get_contents('php://input'), true);
$id     = $pack["id"];          //id of data object
$action = $pack["action"];      //operation type - inserted, updated, deleted
$data   = $pack["data"];        //data object

As a response, the client side expects any valid JSON object which may contain a new id (can be used during data inserting):

//for example

or on returning an error:

{"action":"error" }

You can pass any extra data in a JSON response. Extra attributes can be retrieved on the client side by means of the onAfterUpdate event.

Back to top