dhtlmxGrid can be initialized from an HTML markup. This allows converting existing tables to active grids, or the user can generate data as an HTML table and convert it into the grid, instead of working with a raw XML.
There are two ways to initialize the grid from the HTML:
In both cases, the user needs to add dhtmlxgrid_start.js file to the page.
Note: only one of the above mentioned methods should be used at a time (not both at the same time).
A table can be converted to the grid by dhtmlXGridFromTable command:
<table id="some_table"> <tr> <td>Header A</td><td>Header B</td> </tr> <tr> <td>Data A</td><td>Data B</td> </tr> ... </table> <script> var grid = dhtmlXGridFromTable("some_table"); </script>
Before the initialization:
After the initialization:
The initialization by a CSS class will work only for those tables that were on page at the moment of loading. If the page was created dynamically or loaded by some kind of AJAX, only the direct initialization method can be used.
<table id="some_table" class="dhtmlxGrid"> <tr> <td>Header A</td><td>Header B</td> </tr> <tr> <td>Data A</td><td>Data B</td> </tr> ... </table>
The first row of the table will be used for grid headers. The user can apply some attributes to define properties of grid's columns (all of them are optional).
<table class="dhtmlxGrid"> <tr> <td width="150" align="left">Column 1</td> <td type="co" sort="str">Column 2</td> <td type="edn" format="0,000.00">Column 3</td> <td>Column 4</td> </tr>
The user can indicate:
Some grid modes can be enabled directly from source table attributes:
//enable drag-and-drop in the grid <table class="dhtmlxGrid" dragAndDrop="true" //set path to the image folder of the grid <table class="dhtmlxGrid" imgpath="../some/.." //enable multi-line mode in the grid <table class="dhtmlxGrid" multiline="true" //enable light mouse navigation mode <table class="dhtmlxGrid" lightnavigation="true" //enables the alter CSS mode (it is equal to grid.enableAlterCss("someA","someB")) <table class="dhtmlxGrid" unevenrow="someA" evenrow="someB" //enables the split mode <table class="dhtmlxGrid" split="2"
By default, the grid will take the same size as the table from which it was created. But the user can specify its size directly through source table attributes:
<table class="dhtmlxGrid" gridWidth="600" gridHeight="400"
If the user wants to set grid's size to auto, he can use the default approach:
<script> var grid=dhtmlXGridFromTable("some_table"); grid.enableAutoWidth(true); grid.enableAutoHeight(true); grid.setSizes(); </script>
The following methods were used in the code snippet above:
When the grid is created by dhtmlXGridFromTable command, the reference to the grid object is returned from the method.
In case of initialization by a CSS class, the user can define the name of the global variable that will be used as a reference:
<table class="dhtmlxGrid" name="grid">
It allows to call any API method of such grid by:
grid.some(...
It should be noted that the object becomes available only after initialization (which occurs with onLoad event).
It is possible to attach some custom code to the moment when grid's configuration is being initialized and to the moment when the grid has already been initialized:
<table name="grid" class="dhtmlxGrid" onbeforeinit="myfunc1()" oninit="myfunc2()" ... <script> function myfunc1(){ grid.enableMultiselect(true); } function myfunc2(){ grid.selectRow(0); } </script>
The code of onbeforeinit attribute is executed before grid's structure and data are initialized from the HTML, so the user can place any global setting here. The code of oninit attribute is executed when grid's configuration and data are loaded, so it is the best place for some after-initialization calls - row selecting, data operation, etc.