Strange behaviour - help needed
Strange behaviour - help needed
Hello all
I'm new to dataTables but I've been progressing quite well until now when I'm stuck in a problem. I added this code to save the users' table settings on the server so that they can see the same tables when they work on different computers:
[code]
"fnStateSave": function(oSettings, oData){
$.post("ajax/dispatch.php?module=save_user_settings",
{
"table": "clients",
"value": oData
},
function(data){ console.log(data) } // for me to see if all works well
)
}
[/code]
This works well, the settings are passed to the php program. Now let's read data back:
[code]
"fnStateLoad": function (oSettings, callback) {
$.ajax({
"type":"POST",
"url" : "ajax/dispatch.php?module=load_user_settings",
"async" : false,
"data": {"table": "clients"},
"success": function(data){
if (data=="")
{
ret=false;
}else{
ret=JSON.parse(data);
}
}
});
return ret;
},
[/code]
This also works well, EXCEPT THE VISIBILITY OF COLUMNS. I checked: the visibility values are well stored in the database, but the table is rendered with all the columns visible.
I tried to "manually" set the column visibility properties:
[code]
"fnStateLoadParams": function(oSettings, oData){
cVis=oData.abVisCols;
},
"fnInitComplete": function(){
for (i=0;i
I'm new to dataTables but I've been progressing quite well until now when I'm stuck in a problem. I added this code to save the users' table settings on the server so that they can see the same tables when they work on different computers:
[code]
"fnStateSave": function(oSettings, oData){
$.post("ajax/dispatch.php?module=save_user_settings",
{
"table": "clients",
"value": oData
},
function(data){ console.log(data) } // for me to see if all works well
)
}
[/code]
This works well, the settings are passed to the php program. Now let's read data back:
[code]
"fnStateLoad": function (oSettings, callback) {
$.ajax({
"type":"POST",
"url" : "ajax/dispatch.php?module=load_user_settings",
"async" : false,
"data": {"table": "clients"},
"success": function(data){
if (data=="")
{
ret=false;
}else{
ret=JSON.parse(data);
}
}
});
return ret;
},
[/code]
This also works well, EXCEPT THE VISIBILITY OF COLUMNS. I checked: the visibility values are well stored in the database, but the table is rendered with all the columns visible.
I tried to "manually" set the column visibility properties:
[code]
"fnStateLoadParams": function(oSettings, oData){
cVis=oData.abVisCols;
},
"fnInitComplete": function(){
for (i=0;i
This discussion has been closed.
Replies
Allan
[code]
{"iCreate":"1345532478200","iStart":"0","iEnd":"0","iLength":"25","aaSorting":[["2","asc","0"]],"oSearch":{"bCaseInsensitive":"true","sSearch":"","bRegex":"false","bSmart":"true"},"aoSearchCols":[{"bCaseInsensitive":"true","sSearch":"","bRegex":"false","bSmart":"true"},{"bCaseInsensitive":"true","sSearch":"","bRegex":"false","bSmart":"true"},{"bCaseInsensitive":"true","sSearch":"","bRegex":"false","bSmart":"true"},{"bCaseInsensitive":"true","sSearch":"","bRegex":"false","bSmart":"true"},{"bCaseInsensitive":"true","sSearch":"","bRegex":"false","bSmart":"true"}],"abVisCols":["false","false","true","true","true"]}
[/code]
Version 1.9.1
Yes, state saving works automatically: to cookies. This is what I'd like to alter: to save to the server.
Column position array saves fine. So JSON object is being loaded and saved correctly.
I'm logging the json oData to console so see here in video. Columns display even though aocolumnvis array has half of the values set to false
See what I mean here: http://screencast.com/t/gZTAIp7H4G84
Thanks for the video. I might not be quite understanding - is it the JSON parsing error at the end that is the problem? What is being returned from the server at that point (I'm assuming there will be an error message in it somewhere since it is not valid JSON)?
Are you able to link me to the page you are working on so I can try and debug it?
Thanks,
Allan
We were able to fix this. It turns out our server requests were for whatever reason trying parse the json twice during the state loading. Changing to dataType: html in the load function let us parse it correctly. See below for our load.
[code]
"fnStateLoad": function (oSettings) {
var o;
// Send an Ajax request to the server to get the data. Note that
// this is a synchronous request.
$.ajax({
url: "<?php echo PATH_URL; ?>requests/datatables/savestate.php",
data: {task: 'g',view: '<?php echo $view ?>'},
dataType: "html",
method: "post",
async: false,
success:
function (data) {
if(data != "")
o = JSON.parse(data);
console.log('string returned');
}
});
return o;
},
[/code]
[code]"abVisCols":["false","false","false","false","false","true","true","true","true","true","true","true","true","true","true","true"][/code]
If i load the last state from database the first 5 hidden columns are visible. If i use stateSave with cookies the table works well.
What i can do?
That is a string value which evaluates as `true`. If you use a boolean false, does it work correct?
Allan
The json array i get from database is generated by datatables. So if you're proposal is right the jason array should be fixed? :)
you were right. i changed the json array and set the values to false instead of "false" on my stateload function.
Why my php function json_encode() saves the $_POST array with a "true"/"false" string?
Any suggestions? :)
Because HTTP has no concept of boolean values. "false" is transmitted as simply the ASCII codes for the five letter `false` . You need to either submit them in a JSON stringified value or convert them.
Allan
Now my saved states works like it should. :)