Return custom variables from server side to datatables ajax call

Return custom variables from server side to datatables ajax call

ryan2johnson9ryan2johnson9 Posts: 6Questions: 3Answers: 0

Hi,
I would like to return some non table data from my server-side datatable back to the browser.
https://datatables.net/manual/server-side specifies what I must return(data, recordsTotal, recordsFiltered, etc..) and has some other options like row classes. But how can I add my own variable to the response.

e.g. ryansCustomVariable

and then access this - Iwas going to try getting it in "drawCallback: function(settings){....ryansCustomVariable}"

(Why? I would like to set some info above the table with JQuery. I can calculate the info from the result of the db call in the server side code.)

Ryan,
p.s. I really love the 1.10 api! so clean and easy to use! Thanks

This question has an accepted answers - jump to answer

Answers

  • annoyingmouseannoyingmouse Posts: 2Questions: 0Answers: 1
    Answer ✓

    I've been playing with this all afternoon and after reading about the server-side options and writing my own server-side query I clocked that the way to go was to use the "dataSrc" of the "ajax" object. Basically you'll need to remember to return your data to the datatable but you can listen to whatever you want in there:

    "dataSrc": function (json){
        if(json.ryansCustomVariable){
            $("#some_span").html(json.ryansCustomVariable);
        }
        return json.data;
    }
    

    Hope that that helps.

  • ryan2johnson9ryan2johnson9 Posts: 6Questions: 3Answers: 0
    edited March 2015

    Thanks a million @annoyingmouse - it works!!

This discussion has been closed.