Colvis with server-side stateSaveCallback() is broken

Colvis with server-side stateSaveCallback() is broken

pgerundtpgerundt Posts: 90Questions: 13Answers: 2

Minimal test case:
http://live.datatables.net/jebacowu/5/edit

During init, the visible parameters are submitted as strings (see console).
When changing the visibility of a single column, the visible parameters are submitted as boolean (see console).
Every second click in the "Column visibility" dropdown works.

Replies

  • pgerundtpgerundt Posts: 90Questions: 13Answers: 2

    Workaround (or maybe best practice?):

    stringify the whole data, set content type to application/json and use the request body in the serverside script

    stateSaveCallback: function(settings, data) {
      jQuery.ajax({
        type: 'POST',
        url: '[URL]',
        dataType: 'json',
        contentType: 'application/json',
        data: JSON.stringify({
          data: data
        })
      });
    },
    

    Working example:
    http://live.datatables.net/jebacowu/7/edit

  • allanallan Posts: 63,489Questions: 1Answers: 10,470 Site admin

    Hi Pascal,

    It is because the data being send back from the server from the state load is coming back as strings:

    DataTables doesn't do any type correction - so when the state is next saved (which is almost immediately is for the first draw), those values are used - even although they are invalid.

    In short, the data returned from the server is invalid from the looks of things. I think your workaround handles this nicely as it will preserve the types of the data, and then presumably your PHP script can just dump that whole object into the database - retaining the typing all the way though.

    So yes, do what you've done :). It applies here - it might not apply for everyone, so if anyone else reads this with the same problem, make sure you look at your type handling.

    Allan

  • pgerundtpgerundt Posts: 90Questions: 13Answers: 2

    For anyone else reading this:

    • send your requests with JSON.stringify() and with contentType: 'application/json'
    • in the server side script, do not evaulate $_REQUEST but the request body with
        $request = json_decode(file_get_contents('php://input'), true);
    

    Case closed.

Sign In or Register to comment.