Here is a code excerpt from a dhtmlxgrid sample file:
<p> You are allowed to clear data and structure of grid and then load new data from XML file.</p>
<a href="#" onclick="savegrid();">Save Grid</a>
<div id="gridbox" style="width:600px; height:270px; background-color:white;"></div>
<a href='#alfa' onClick="ser()">Reload grid with another structure</a>
<br>
<script>
mygrid = new dhtmlXGridObject('gridbox');
mygrid.setImagePath("../../codebase/imgs/");
mygrid.loadXML("../common/grid500.xml");
function ser(){
mygrid.clearAll(true);
mygrid.loadXML("../common/gridH3.xml");
}
function savegrid(){
// want to save the grid here
}
</script>
If I edit data in the grid. I’d like to be able to save it to file. How do I do that? This isn’t done automatically.
It is possible to get a csv or an XML of the dhtmlxgrid current data and have scripts to save the updates to a database or an xml file. The dhtmlxgrid method to load a csv into the grid is
.loadCSVString(). The method to load an xml file is.loadXML(). The method to create a csv string with the current grid content is.serializeToCSV(). The method to create an xml with the current grid content is.serialize().Here is a sample use of the grid. The data to be shown and edited in the grid is stored in an XML file. The updates/edits made in the grid must also be stored in that XML file. Since my XML file has a different definition than the XML that dhdmlxgrid uses, I extract the data from the XML as a csv string, load the string into the grid, then extract the updates as a csv string to save back to the XML. The code to do that is simpler than switching between the two xml formats.
HTML
One div is defined to be the grid element. Save and Cancel buttons are also defined.
Grid Initialization
In this javascxript the grid object is created and filled with data.
Save Data
In this script the grid content are retrieved and saved to XML.