Cookies: non-valid JSON and search terms

Cookies: non-valid JSON and search terms

John ArcherJohn Archer Posts: 56Questions: 1Answers: 0
edited December 2011 in Bug reports
Hi Allan,

while implementing my server side cookie saving mechanism I encountered the following problems:

1. I use fnStateSaveCallback to send sValue to a PHP script, which saves the string in a file on the webserver. I wondered why loading the file and creating the proper cookie always fails. So I console.log'ed sValue and encountered, that this string not a valid JSON string, as the closing curly bracket (}) at end is missing. E.g. I get the following output
[code]{"iCreate":1323350034606,"iStart":0,"iEnd":0,"iLength":10,"sFilter":"","sFilterEsc":true,"aaSorting":[ [3,"desc"]],"aaSearchCols":[ ["",true],["",true],["",true],["",true]],"abVisCols":[ false,false,false,true][/code]
This is quite bad for me, as I would have to add the } manually at the end of the string to make the string valid for later loading.

2. It is cool that search terms in filter boxes are also saved. After loading they are correctly applied to the table, so I see the filtered result. But unfortunately the search term(s) is/are not put automatically in the filter box(es), at least not here. Should it work that way? I am not sure whether something is wrong with my code or if I really have to handle that manually.

Thanks in advance!

Regards!

Replies

  • allanallan Posts: 63,383Questions: 1Answers: 10,449 Site admin
    Hi John,

    1. Yes this is a known "issue" and is noted in the documentation for fnStateSaveCallback. Basically jQuery doesn't have stringify function (at least not that I'm aware of), so unfortunately the "object" is built up as a string. Its pants, but without requiring another library to crew really see a huge amount of other options - since the whole point of the callback was to let people easily add extra options (i.e. currently they don't have to strip the brace at the end).

    2. This should work, as seen in this example: http://datatables.net/release-datatables/examples/basic_init/state_save.html

    Allan
  • John ArcherJohn Archer Posts: 56Questions: 1Answers: 0
    1. Grrr .... You are allowed to RTFM me! :-) Sorry for overseeing this. :-(

    2. Ok, thanks! Then I have to look in my code where the problem is.

    Thanks again!

    John
  • allanallan Posts: 63,383Questions: 1Answers: 10,449 Site admin
    Lol - no worries. There are so many options for DataTables these days that I'm probably the greatest user of my own documentation...

    Allan
  • John ArcherJohn Archer Posts: 56Questions: 1Answers: 0
    :-)
  • John ArcherJohn Archer Posts: 56Questions: 1Answers: 0
    Hi Allan,

    2. - I looked at your example. Using the _global_ filter works there and also for my project, which means the search term is put into the input field again when the cookie is used.
    But it does not work for input fields which are below (or above) each column! Should it work there, too? Do you have an example for that?

    Thanks a lot!

    Regards!

    PS: Will test the new StateLoad event now! *excited* :-)
  • allanallan Posts: 63,383Questions: 1Answers: 10,449 Site admin
    > But it does not work for input fields which are below (or above) each column! Should it work there, too?

    No. The reason why not is that these are external controls that DataTables has no control over. It hasn't put these elements on the page, it doesn't do anything with them, the only link is your call to fnFilter. As such, it is not possible for DataTables to restore the values in these fields, since it doesn't know about them!

    The way to deal with this is that you would need to loop over the fnSettings().aoPreSearchCols[] array and read the 'sSearch' property for each object in the array (the array is equal in length to the number of columns) and result the values in your controls (this also allows flexibility for the control since it could be a slider, or select or anything else).

    Regards,
    Allan
  • John ArcherJohn Archer Posts: 56Questions: 1Answers: 0
    edited December 2011
    Cool, thanks a lot for all the hints! I will look into that.

    EDIT: Great, works like a charm!

    Thanks a lot!
This discussion has been closed.