Check documentation for the latest version of dhtmlxSuite Formatting/Changing Data Before Loading DHTMLX Docs

Formatting/Changing Data Before Loading

Basic formatting methods

APPLICABLE TO: Grid, TreeGrid, Tree, Combo, Scheduler, DataView, Chart, Form, DataStore components

When you need to update the values which were returned from a database table or set some specific formatting before sending data to the client side, you should use the beforeRender event handler.

Common usage

$res= new PDO("mysql:dbname=$mysql_db;host=$mysql_server",$mysql_user,$mysql_pass);
require("../../codebase/grid_connector.php");   
 
function color_rows($row){
    if ($row->get_index()%2)
    $row->set_row_color("red");
}
 
$grid = new GridConnector($res);
$grid->event->attach("beforeRender","color_rows");
$grid->render_table("grid50000","item_id","item_nm,item_cd");
  • color_rows function sets colors for rows subject to their indices
  • during the data generation, for each record outputted for the client side the beforeRender event will be executed, i.e. the color_rows function will be called for each record
  • $row is an instance of GridDataItem object related to the current record

Data formatting

APPLICABLE TO: Grid, TreeGrid, Tree, Combo, Scheduler, DataView, Chart, Form, DateStore components

function formatting($row){
    //render a field as details link
    $data = $row->get_value("some_field");
    $row->set_value("some_field","<a href='details.php?id={$data}'>Details</a>");
 
    //formatting a date field
    $data = $row->get_value("other_field");
    $row->set_value("other_field",date("m-d-Y",strtotime($data)));
}
 
$grid = new GridConnector($res);
$grid->event->attach("beforeRender","formatting");
  • The get_value and set_value methods allow you to get or set value for any field related to the record (it doesn't affect actual values in DB)
  • If an alias was used during the data configuration, you need to use it instead of the real db field name as the first parameter of the get/set command.

Using extra fields

APPLICABLE TO: Grid, TreeGrid, Tree, Combo, Scheduler, DataView, Chart, Form, DataStore components

More complex formatting rules can be defined by using extra fields during the configuration.

function formatting($row){
    //set row color
    $row->set_row_color($row->get_value("color"));
    //save in userdata 
    $row->set_userdata("some_data",$row->get_value("count"));
}
 
$grid = new GridConnector($res);
$grid->event->attach("beforeRender","formatting");
$grid->render_table("some_table","id","name,price","color,count");
  • field color isn't outputted to the client side, but is used to define the property of a row.
  • during the update/insert operation only the 'name' and 'price' columns can be changed, the 'color' one will stay untouched.
  • the 'count' field will be sent to the client side as userdata of the row, and it will be possible to access it on the client-side through the related data.

Tree/Treegrid specificity

APPLICABLE TO: Tree, TreeGrid

TreeGrid provides TreeGridDataItem and Tree provides TreeDataItem as the input parameter of the beforeRender event handler. Both of them support base operations and a few specific ones.

function custom_format($item){
    if ($item->get_value("complete")>75) 
        $item->set_check_state(1);
 
    if ($item->get_value("duration")>10)
        $item->set_image("true.gif");
    else
        $item->set_image("false.gif");
}
$tree->event->attach("beforeRender","custom_format");
  • the set_image method allows setting an image of a tree element (for treegrid it accepts the only parameter, while for tree there can be 3 different images for 3 states of a tree's item)
  • the set_check method exists only in TreeDataItem object and allows setting the state of the related checkbox (tree needs to have checkboxes enabled in js. configuration code as well)
  • the beforeRender event can be used in dynamic Tree and TreeGrid to define which elements of hierarchy are branches and which ones are leafs (see details here).

While deleting items that have children you can face the problem that the parent item is deleted but its children are not. In this case you should use the beforeDelete event:

function beforeDeleteFunc( $data ) {
   // custom logic
};
$conn->event->attach("beforeDelete","beforeDeleteFunc");
Back to top