Strange behaviour - help needed

Strange behaviour - help needed

neilsneils Posts: 8Questions: 0Answers: 0
edited August 2012 in General
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

Replies

  • allanallan Posts: 63,180Questions: 1Answers: 10,411 Site admin
    Don't know off the top of my head I'm afraid. What does the save JSON 'string' look like? State saving should be saved in DataTables automatically. Which version are you using?

    Allan
  • neilsneils Posts: 8Questions: 0Answers: 0
    edited August 2012
    Hi, thank you for your kind support! This is an example of the JSON what we get at fnStateLoad:
    [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.
  • neilsneils Posts: 8Questions: 0Answers: 0
    It seems that fnSetColumnVis will result to an error if you try to set the value TRUE for an already visible column!
  • drewtdrewt Posts: 22Questions: 0Answers: 0
    edited May 2013
    Any word on this? I think I am having the same problem.
    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
  • allanallan Posts: 63,180Questions: 1Answers: 10,411 Site admin
    Hi drewt,

    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
  • drewtdrewt Posts: 22Questions: 0Answers: 0
    Hey all,

    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]
  • DivDaxDivDax Posts: 4Questions: 0Answers: 0
    I have the same problem. The json data are saved to my database with
    [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?
  • allanallan Posts: 63,180Questions: 1Answers: 10,411 Site admin
    > "false"

    That is a string value which evaluates as `true`. If you use a boolean false, does it work correct?

    Allan
  • DivDaxDivDax Posts: 4Questions: 0Answers: 0
    I don't know but i think you're right.
    The json array i get from database is generated by datatables. So if you're proposal is right the jason array should be fixed? :)
  • DivDaxDivDax Posts: 4Questions: 0Answers: 0
    Hey Allan,

    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? :)
  • allanallan Posts: 63,180Questions: 1Answers: 10,411 Site admin
    > Why my php function json_encode() saves the $_POST array with a "true"/"false" string?

    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
  • DivDaxDivDax Posts: 4Questions: 0Answers: 0
    Thank you allen! Now i've edited the complete json array with php and set the correct values.
    Now my saved states works like it should. :)
This discussion has been closed.