DHTMLX Docs & Samples Explorer

Index
dhtmlxconnector
Formatting/Changing Data before Loading

## Formatting/Changing Data before Loading

### Base Formatting Methods

In case you need to update values which were returned from database table or set some specific formatting before sending them to client side, you can use the “beforeRender” event handler.

Common use-case will be similar to next

    	$res=mysql_connect($mysql_server,$mysql_user,$mysql_pass);
    	mysql_select_db($mysql_db);
    	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");

Here , color_rows function attached to the beforeRender event and set colors for rows based on their index.

During data generation, for each record outputed for client side beforeRender event will be executed, which means color_rows function will be called for each record. $row is an instance of GridDataItem object, related to current record.

Same approach can be used for data formating

    	function formatting($row){
                      //render field as details link
                      $data = $row->get_value("some_field");
                      $row->set_value("some_field","<a href='details.php?id={$data}'>Details</a>")

                      //formatting 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");

get_value and set_value method allow get or set value of any field related to the record ( it not affect actual values in DB ), if alias was used during data configuration - you need to use it instead of real db field name as first parameter of get|set command.

More complex formating rules can be defined by using “extra” fields in configuration, fields which will not be outputed to client but can be used inside events.

    	function formatting($row){
                      //set row color
                      $row->set_row_color($row->get_value("color"));
                      //save in userdata
                      $row->set_user_data("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");

Here, field color not outputed to client side , but used to define property of row. During update|insert operation only name and price columns may be changed, color will stay untouched. Count field will be sent to client side as userdata of the row ( and it will be possible to access it on client side through related data.

### Formatting in Combo

In case of dhtmlxCombo event works the same but provides ComboDataItem as parameter of event, it possible to implement the same use-cases with it

### Formatting in TreeGrid and Tree

TreeGrid provides TreeGridDataItem and Tree provides TreeDataItem for beforeUpdate event, both of them support base operations and 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");

- set_image method allows to set image of tree element ( for treegrid it accepts only one parameter, while for tree it can be a single image of 3 different images for 3 states of tree's item.
- set_check method exists only in TreeDataItem object and allow to set state of related checkbox ( tree need to have checkboxes enabled in js. configuration code as well )

beforeRender event can be used in dynamic Tree and TreeGrid to define which elements of hierarchy are branches and which are leafs. It is covered in related section of document.