DHTMLX Docs & Samples Explorer

Cookies Manipulation

The user should include dhtmlxgrid_ssc.js file into the HTML in order to be able to manipulate with cookies.

Enabling Automatic Saving to Cookies

The following method can be used for this purpose:

    <script>
        grid.enableAutoSizeSaving(name,cookie_param); // automatic saving of columns sizes
        grid.enableAutoHiddenColumnsSaving(name,cookie_param); // automatic saving of columns state (hidden|shown)
        grid.enableAutoSaving(name,cookie_param); // automatic saving of all possible parameters
        grid.enableOrderSaving(name,cookie_param); // automatic saving of columns order
        grid.enableSortingSaving(name,cookie_param); // automatic saving of sorting state
    </script>
 

The parameters of all the above mentioned methods are the following:

  • name - optional, cookie name;
  • cookie_param - additional parameters added to cookie.

Saving to Cookies

There are several methods in dhtmlxGrid that are responsible for saving to cookies:

    <script>
        grid.saveHiddenColumnsToCookie(name,cookie_param); // save columns state (hidden|shown) to cookies
        grid.saveOrderToCookie(name,cookie_param); // save sorting order to cookies
        grid.saveSortingToCookie(name,cookie_param); // save sorting to cookies
        grid.saveSizeToCookie(name,cookie_param); // save column width to cookies
    </script>
 

The parameters of all the above mentioned methods are the following:

  • name - optional, cookie name;
  • cookie_param - additional parameters added to cookie.

Loading from Cookies

There are also several script methods for loading from cookies:

    <script>
        grid.loadHiddenColumnsFromCookie(name); // load columns state (hidden|shown) from cookies
        grid.loadOrderFromCookie(name); // load sorting order from cookies
        grid.loadSizeFromCookie(name); // load grid layout from cookies
        grid.loadSortingFromCookie(name); // load sorting order from cookies   
    </script>
 

The parameter of all the above mentioned methods is name - optional, cookie name.

Clearing Cookies

The method responsible for clearing cookie with grid configuration details is the following:

    <script>
        grid.clearConfigCookie(name);
    </script>
 

The parameter of this method is name - optional, cookie name.

Calling order

While using automatic saving remember the following rules:

  1. 'enable' and 'load' methods are called after grid initialization but before data loading;
  2. if you need to restore several cookie data you should follow a specific calling order. The correct order is shown in the code snippet below;
  3. 'enable' methods must be called only AFTER calling the 'load' methods.
mygrid = new dhtmlXGridObject('gridbox');
...
mygrid.init(); //firstly, initialize the grid
 
 
mygrid.loadOrderFromCookie(); //then, restore the required data
mygrid.loadSizeFromCookie();
mygrid.loadSortingFromCookie();
mygrid.loadHiddenColumnsFromCookie();
 
 
mygrid.enableAutoSizeSaving(); //enable saving cookies 
mygrid.enableSortingSaving();
mygrid.enableOrderSaving();
mygrid.enableAutoHiddenColumnsSaving();
 
mygrid.loadXML("data.php"); //load data to the grid