DHTMLX Docs & Samples Explorer

Grid Serialization

Serialization to XML

Serialization methods allows getting the grid in XML representation (XML string). Various levels of serializations define the number of grid parameters reflected in the resulting XML:

        grid.setSerializationLevel(userData, selectedAttr, config, changedAttr, onlyChanged, asCDATA);
        grid.setSerializableColumns(list);
        grid.enableMathSerialization(status);
        var myXmlStr = grid.serialize();
 

The methods used in the snippet above are as follows:

  • setSerializationLevel(userData, selectedAttr, config, changedAttr, onlyChanged, asCDATA) - configures XML serialization with the following methods:
    • userData - enable/disable user data serialization
    • selectedAttr - include “selected” row's attribute in result XML
    • config - serialize grid configuration (only information about grid structure)
    • changedAttr - include “changed” cell's attribute in result XML
    • onlyChanged - include only changed rows in result XML
    • asCDATA - output cell values as CDATA sections (prevent invalid XML).
  • setSerializableColumns(list) - configures which column must be serialized (if the user does not use this method, then all columns will be serialized); the parameter is:
    • list - list of true|false values separated by comma; if the list is empty, all fields will be serialized.
  • enableMathSerialization(status) - enables/disables serialization of math formulas; the parameter here is:
    • status - true|false;
  • serialize() -gets actual XML of the grid.

Control over XML Serialization

Tags used for rows and cells during the serialization can be defined as:

        grid.xml.s_row="myrowtag";
        grid.xml.s_cell="mycelltag";
 

The grid uses some predefined set of attributes that will be included in the serialization. But if the user applies some custom attributes, he can force their including in the serialization.

        grid.setRowAttribute(id,"custom1","data");
        //...
        grid.xml.row_attrs.push("custom1");                // will include custom1 in the serialization
 
        grid.cells(i,j).setAttribute("custom2","data");
        //...
        grid.xml.cell_attrs.push("custom2");                // will include custom2 in the serialization

Serialization to CSV

Serialization to CSV became possible since v1.2:

        grid.csv.cell="\t"; // change CSV delimiter
        var csvNew = grid.serializeToCSV(); // serialize to CSV with the current active delimiter
        grid.parse(csvNew,"csv"); // load grid from a CSV string
           // the following code is still possible to use:
        grid.setCSVDelimiter("\t"); // change CSV csv delimiter
 
        var csvNew = grid.serializeToCSV(); // serialize to CSV with the current active delimiter
 
        grid.loadCSVString(csvNew);//load grid from a CSV string
</script>

The following method is responsible for the mode in which id for rows loaded from CSV is autogenerated:

          grid.enableCSVAutoID(true|false);

Submitting Serialization as a Part of Form

There is a method that is responsible for including serialized grid as a part of form submitting:

        grid.submitSerialization(true|false);

The user should not forget to include dhtmlxgrid_form.js file into the page.