How to get data retrieved by the previous draw in drawCallback

How to get data retrieved by the previous draw in drawCallback

hafanhafan Posts: 5Questions: 2Answers: 0

Currently I am working on the server-side processing stuff.
Looking at the doc for the drawCallback, one thing I notice is that: unlink the initComplete or ajax.reload, the drawCallback doesn't provide a parameter json, which is the data just received for the previous draw.

What I intend to do is: every time when dataTable sends a ajax draw request to the server, my server will reply back the data as well as a timestamp that records the time that this request is received by the server. After each draw, I want to access this timestamp which is contained in the json variable sent from the server. However, since drawCallback does not have a parameter for it, I am not sure how to do that

What I want is something like

drawCallback : function(setting, json) {
 
 // used the timestamp sent in the json, I am able to update the information shown on the page for every draw event
 $('#time').text(json.timestamp);

}

Right now, the way I manage to get the same goal is via ajax.dataSrc, in which I do

dataStrc: function (json) {

  $('#time').text(json.timestamp);

  return json.tableData; // array of data used to draw the table
}

However, I am not sure whether using dataSrc as a callback for the draw event will always be equivalent to use the drawCallback which is designed to serve as the callback for the draw

I need a function to be fired after each draw has been completed, preferably as a callback, and I can also access the data obtained by the previous draw request in this function.

Thanks

Answers

  • kthorngrenkthorngren Posts: 21,243Questions: 26Answers: 4,929

    My first thought was to use ajax.dataSrc and store the timestamp in a global variable. But Allan says that it doesn't always work reliable with server side processing. However if it works then use it.

    Another option will allow you to use drawCallback. You can get an API instance in drawCallback then use the ajax.json() API to get the JSON returned.

    I tried it here and it works:
    http://live.datatables.net/bodisuzu/1/edit

    Either approach should work despite what Allan says :smile:

    Kevin

  • Bindrid2Bindrid2 Posts: 79Questions: 4Answers: 12

    If all you are interested in is the time stamp then I would do this

    dataFilter : function(response) {
        var data = JSON.parse(response);
        sessionStorage.setItem("timestamp", data.timestamp);
    },
    ajax: {
        url: blahblah,
        data: function(req){
            req.timestamp = sessionStorage.getItem("timestamp");
            return req;
        }
    }
    
This discussion has been closed.