dhtmlxconnector:component-specific_how-tos_grid [DHX documentation]

PHP Connector DHTMLX

Component-specific how-tos (grid)

How can I define grid structure on server-side?

To define grid structure on server-side you can use one of two modes:

  • automatic
  • manual

In both modes you should use set_config() method where as input parameter GridConfiguration object must be specified.

In automatic mode, grid will use names of table's fields as labels for the columns.

$config = new GridConfiguration();
$grid->set_config($config);
$grid->render_table("grid50000","item_id","item_nm,item_cd");

In manual mode, structure is defined by php command. Names of commands mimic names of js commands with similar functionality.

$config = new GridConfiguration();
$config->setHeader(array("column 1","column 2"));
$config->setColTypes(array("ro","ed"));
$grid->set_config($config);
$grid->render_table("grid50000","item_id","item_nm,item_cd");

For more information of the topics covered here, see 'defining grid structure on server-side' guide.

How can I export grid's data from server to Excel file?

To export data to Excel file you should:

  1. Include one additional file

    require("../../../codebase/convert.php");
  2. Activate conversion service

    $convert = new ConvertService("http://dhtmlx.com/docs/products/devExchange/samples/grid2excel_02/server/generate.php");
  3. Start exporting

    $convert->excel("some.xls",false);

    Both method's parameters are optional. The first parameter is a name of output file. The second parameter specifies how file will be exported: true - as inline content, false - as individual file.

After you call the method excel(), service automatically will start to export data defined through GridConnector.

For more information of this topic, see 'Data export' guide.

How can I export grid's data from server to pdf file?

To export data to pdf file you should:

  1. Include one additional file

    require("../../../codebase/convert.php");
  2. Activate conversion service

    $convert = new ConvertService("http://dhtmlx.com/docs/products/devExchange/samples/grid2pdf_02/server/generate.php");
  3. Start exporting

    $convert->pdf("some.pdf",false);

    Both method's parameters are optional. The first parameter is a name of output file. The second parameter specifies how file will be exported: true - as inline content, false - as individual file.

After you call the method pdf(), service automatically will start to export data defined through GridConnector.

For more information of this topic, see 'Data export' guide.

How can I populate select/combo columns with data?

To define options of select/combo columns you have 2 ways:

  1. to load data from the same table the grid is populated with data from

    $grid->set_options("item_nm",array("1" => "one", "2"=>"two","3" => "three")); // value => label
    $grid->render_table("grid50","item_id","item_nm,item_cd");
  2. to load data from another table

    $options = new OptionsConnector($res);
    $options->render_table("countries","country_id","country_id(value),country_name(label)");
    $grid->set_options("item_nm",$options);
     
    $grid->render_table("grid50","item_id","item_nm,item_cd");

For more information of this topic, see 'Select/combobox columns in grid' article.

How can I load data from a table that doesn't contain identity field?

Using KeyGridConnector instead of GridConnector, you can load data from a table without identity field. In this case, any data field will serve as identity.

$grid = new KeyGridConnector($res);
$grid->render_table("mytable","name","name,address,phone");

For more details, see 'KeyGridConnector' guide.

How can I set a custom style for a row or a cell?

dhtmlxConnector contains a bit of methods that allow to set the appearance of a grid.
These methods can be divided into 2 groups:

for a cell customization:

for a row customization:

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("records","item_id","item_nm,item_cd");

Tips:

  • to overwrite background you should use 'background' not 'background-color' attribute.
  • skin's css can overwrite a number of css attributes. To avoid it - use the !important flag.

    $row->set_row_attribute("class","backrgroundclass");
    .backrgroundclass{
       background:red !important;
    }