Author Topic: saveState and loadState from database  (Read 2323 times)

zekinash

  • Newbie
  • *
  • Posts: 1
    • View Profile
saveState and loadState from database
« on: February 22, 2017, 03:39:53 pm »
We are testing the option to save and load the state in 3.3.0 with some difficulty. We want to save it in the database. We do it so:
Code: [Select]
          {
            type: 'button',
            label: 'Save State',
            listener: function(){
              var state = this.saveState();
              saveState(state);
            }
          },
          {
            type: 'button',
            label: 'Load State',
            listener: function(){
              loadState(this);
            }
          }
...more code...
function saveState( state)
    {
      $.ajax({
          url : 'savestate.php',
          data : state,
          type : 'POST',
          dataType : 'json',
          success : function(json) {
              console.log(json.message);
          },
          error : function(xhr, status) {
              console.log(xhr);
          },
          complete : function(xhr, status) {
              console.log(xhr);
          }
      });
    }

function loadState(grid)
    {
      $.ajax({
          url : 'getState.php',
          type : 'GET',
          dataType : 'json',
          success : function(json) {
              var colModel = json.data;
              grid.loadState(colModel, 'refresh');
              console.log(colModel);
          },
          error : function(xhr, status) {
              console.log(xhr);
          },
          complete : function(xhr, status) {
              console.log(xhr);
          }
      });
    }

The status I want to save to the database is:
Code: [Select]
{"colModel":[{"width":150,"dataIndx":"nombre","filter":{}},{"width":60,"dataIndx":"pcod","filter":{}},{"width":35,"dataIndx":"pemp","filter":{}},{"width":35,"dataIndx":"pserie","filter":{}},{"width":90,"dataIndx":"pfeccar","filter":{}},{"width":75,"dataIndx":"phorcar","filter":{}},{"width":90,"dataIndx":6,"filter":{}},{"width":90,"dataIndx":7,"filter":{}},{"width":150,"dataIndx":"plugcar","filter":{}},{"width":90,"dataIndx":"pfecdes","filter":{}},{"width":75,"dataIndx":"phordes","filter":{}},{"width":90,"dataIndx":11,"filter":{}},{"width":90,"dataIndx":12,"filter":{}},{"width":150,"dataIndx":"plugdes","filter":{}},{"width":80,"dataIndx":"pmat","filter":{}},{"width":100,"dataIndx":"pmatcis","filter":{}},{"width":150,"dataIndx":"pnveh","filter":{}},{"width":150,"dataIndx":"pncondu","filter":{}},{"width":150,"dataIndx":"pncte","filter":{}},{"width":95,"dataIndx":"prefcli","filter":{}},{"width":95,"dataIndx":"prefcar","filter":{}},{"width":140,"dataIndx":"pprod","filter":{}}],"height":620,"datestamp":1487757576073,"width":"auto","groupModel":{"dataIndx":[],"dir":[],"collapsed":[]},"pageModel":{"rPP":10,"curPage":1},"sortModel":{"sorter":[]},"freezeRows":0,"freezeCols":2}
[\code]

When saving to database, backslashes are saved:
[code]
{"{\"colModel\":":{"{\"width\":150,\"dataIndx\":\"nombre\",\"filter\":{}},{\"width\":60,\"dataIndx\":\"pcod\",\"filter\":{}},{\"width\":35,\"dataIndx\":\"pemp\",\"filter\":{}},{\"width\":35,\"dataIndx\":\"pserie\",\"filter\":{}},{\"width\":90,\"dataIndx\":\"pfeccar\",\"filter\":{}},{\"width\":75,\"dataIndx\":\"phorcar\",\"filter\":{}},{\"width\":90,\"dataIndx\":6,\"filter\":{}},{\"width\":90,\"dataIndx\":7,\"filter\":{}},{\"width\":150,\"dataIndx\":\"plugcar\",\"filter\":{}},{\"width\":90,\"dataIndx\":\"pfecdes\",\"filter\":{}},{\"width\":75,\"dataIndx\":\"phordes\",\"filter\":{}},{\"width\":90,\"dataIndx\":11,\"filter\":{}},{\"width\":90,\"dataIndx\":12,\"filter\":{}},{\"width\":150,\"dataIndx\":\"plugdes\",\"filter\":{}},{\"width\":80,\"dataIndx\":\"pmat\",\"filter\":{}},{\"width\":100,\"dataIndx\":\"pmatcis\",\"filter\":{}},{\"width\":150,\"dataIndx\":\"pnveh\",\"filter\":{}},{\"width\":150,\"dataIndx\":\"pncondu\",\"filter\":{}},{\"width\":150,\"dataIndx\":\"pncte\",\"filter\":{}},{\"width\":95,\"dataIndx\":\"prefcli\",\"filter\":{}},{\"width\":95,\"dataIndx\":\"prefcar\",\"filter\":{}},{\"width\":140,\"dataIndx\":\"pprod\",\"filter\":{}}":""}}
[\code]

However, if I save it manually correctly, it does not load the state correctly either. I always load the state saved in the browser. How can I store the state in the database and load it without problems?