Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery Datatables return additional information from server

using JQuery Datatables and all is going well.

I've worked out how to send addtional information from the client to the server. Now, I want to go back the other way.

So, how do I send extra information from the server to the client. I would have thought I could add an extra entry in the returned JSON and pull it out somewhere. One item I'd perhaps like to send back is how long the server took to handle the response. I can then show this information to the user.

Any help would be much appreciated. Thanks

like image 769
Lee Avatar asked May 22 '11 19:05

Lee


1 Answers

I think you got quite everything right. You just need to attach the additional data server side in the JSON object and then get it in "fnServerData". You can add this code to the inizialization object:

        "fnServerData": function ( sSource, aoData, fnCallback ) {
            $.getJSON( sSource, aoData, function (json) {
//Here you can do whatever you want with the additional data
                console.dir(json);
//Call the standard callback to redraw the table
                fnCallback(json);
            } );
        }

Server side you can add as many parameters as you want: Usually you have a json with 3 parameters "iTotalRecords" (total number of rows), "iTotalDisplayRecords" (Filtered total if you are using filters) and aaData (An associative array with the rows). If you add for example "iProcessingTime" (time it took to process server side) You could do:

        "fnServerData": function ( sSource, aoData, fnCallback ) {
            $.getJSON( sSource, aoData, function (json) {
//take the processing time and put it in a div
                $('#processingTime').html(json.iProcessingTime);
//pass the data to the standard callback and draw the table
                fnCallback(json);
            } );
        }

Is this what you need?

like image 64
Nicola Peluchetti Avatar answered Oct 10 '22 07:10

Nicola Peluchetti