jQWidgets Forums
jQuery UI Widgets › Forums › Grid › Grid saving state to database
Tagged: jquery grid, jqwidgets
This topic contains 8 replies, has 3 voices, and was last updated by punkrack 11 years, 9 months ago.
-
Author
-
Hello, I’m saving the state of the grid to the database using JSON.stringify(state). However, when I’m attempting to load the state
var thisstate = JSON.parse(data);
$(“#myGrid”).jqxGrid(‘loadstate’, thisstate);It is not loading the previous state.
What am I missing here? Please advise.
Thank you!
Hi vmanne,
1. Do you call ‘loadstate’ in the Grid’s ready callback?
2. Is that state parsed correctly?
3. Which version do you use?
4. Could you provide a sample which demonstrates that?Best Regards,
Peter StoevjQWidgets Team
http://www.jqwidgets.comThanks Peter for your reply.
1. I’m calling loadstate when I’m making customization to Grid (such as when a checkbox is selected to show/hide columns)
2. The state is parsed correctly. When I do a console.log for state before persisting to db is same as one before getting from db. (As seen by Firefox Firebug Window->Object
3. ‘m using the latest version (jQWidgets v2.8.0 (2013-Mar-22))Code snippets
//Saving data:
function SaveState(state) {
console.log(“GridState”, state);
var dataToSend = {
UserId: $(‘#UserId’).val(),
State: JSON.stringify(state) //<—- using JSON.stringify(state) to convert object into string for db storage
};$.ajax({
url: 'xx/SaveUserSettings/?UserId=' + dataToSend.UserId + '&SettingName=asGrid&SettingValue=' + dataToSend.State,
type:'POST',
data: dataToSend,
success: function (data) {
//alert('saved data ' + data);
}
});
}//Getting data:
//get 'views' info for the grid
$.ajax({
url: 'xx/GetUserSettings/?UserId=' + $('#UserId').val() + '&settingName=AssignmentsGrid',
success: function (data) {
var thisstate = JSON.parse(data); //<—- using JSON.parse(data) to convert string from db to object
console.log(thisstate);
//attempting to load the state
$("#asGrid").jqxGrid('loadstate', thisstate);
$("#asGrid").jqxGrid('refresh');
$("#asGrid").jqxGrid('render');
}
});Hi,
The following lines are not correct:
$("#asGrid").jqxGrid('loadstate', thisstate);$("#asGrid").jqxGrid('refresh');$("#asGrid").jqxGrid('render');
To load state, you should use only the ‘loadstate’ method.
$("#asGrid").jqxGrid('loadstate', thisstate);
If the thisstate is in the same format as returned from “getstate” method of jqxGrid, the widget will load it. The same is demonstrated in the Save/Load state sample.
Also make sure that you have a reference to jqxgrid.storage.js.
Best Regards,
Peter StoevjQWidgets Team
http://www.jqwidgets.comHi Peter,
thisstate is the serialized form of object (state) which the “savestate” method gives out.
var state = $("#asGrid").jqxGrid('savestate');
Instead of ‘savestate’ , should I use ‘getstate’ ?
Is this correct way of saving state info to the database and fetching it back?
var state = $("#asGrid").jqxGrid('getstate');
Store state as string
JSON.Stringify(state)
to the database and then use
//statefromdb obj comes from db (JSON string)var thisstate =JSON.parse(statefromdb ); $("#asGrid").jqxGrid('loadstate', thisstate);
After much testing I can confirm that object returned from getstate is same as object returned from the db. Not sure why the loadstate
("#asGrid").jqxGrid('loadstate', <em>thisStateFromDB</em>);
does not work
Hi vmanne,
From the provided information, I do not know what could be wrong on your side.
In case you wish us to debug your scenario, please provide a sample which demonstrates it.Best Regards,
Peter StoevjQWidgets Team
http://www.jqwidgets.comHi Peter, I’ll put together a sample project to illustrate this.
Hi!
If it may be of any help, this is how I got done saving the state in DB and then loading
function saveState() { var theURL = "your-url"; var getState = $("#jqxgrid").jqxGrid('savestate'); var stateToSave = JSON.stringify(getState);// YOU ARE SAVING A STRING $.ajax({ type: "POST", url: theURL, data: { state_save: stateToSave }, success: function(response) { alert("response = " + response); // STRING IS INSERTED IN DB WITH QUOTES (") AND {} // EX : {"width":"100%","height":400... }}); } function loadState() { var theURL = "your-url"; $.ajax({ type: "GET", // OR POST WHATEVER... url: theURL, data: { what: 'ever-you-need' }, success: function(response) { if (response != "") { // IMPORTANT HERE. IN PHP, THE FUNCTION MUST BE RETURNED LIKEWISE://return stripslashes(html_entity_decode($result[0]["state"]));// WHERE $result[0]["state"] IS THE STRING INSERTED IN THE DB response = JSON.parse(response); // IF YOU alert(response) YOU SHOULD GET AN [object Object] AND NOT A STRING. IF YOU GET A STRING SUCH HAS "{"width":"100%","height":400..." IT IS WRONG. $("#jqxgrid").jqxGrid('loadstate', response); } } }); }
Hope it will help.
Version 3.0.2 -
AuthorPosts
You must be logged in to reply to this topic.