5

I have worked on one issue for last two days. I'm using jQuery and DataTables in a page, which fetches data from a server and populates tables quite happily. I need to make a change for requests from dataTables library to fetch data from the server happen.

Is it possible to make a jquery ajax post request without defined param list mention in link below?

https://datatables.net/usage/server-side

This is my json POST request:

{"page_number":1,"page_size":10}

and this is my server response:

{
 "status": 200,
 "message": "Users retrieved successfully.",
  "users": [Assumelistofusers],
  "total_count": 50,
  "total_page_count": 5
}

I have found that, in YUI Library for DataTable Control (beta), there is good example of server side pagination. refer the link:

http://examples.mashupguide.net/lib/yui_2.3.0/examples/datatable/dt_serverpagination.html

1 Answer 1

2

I am not sure why you want to do this, because you will loose all the filtering and sorting if you explicitly exclude these parameters that are there for a reason.

But, yes, you can use the fnServerData function to define you own calls and methods to your server sided datasource.

$('#dt').dataTable({
"bServerSide": true,
"sAjaxSource": "my_serverdata.script",
"fnServerData": function(sSource, aoData, fnCallback, oSettings) {
  var mydata=[]
  mydata.push( { "name": "page_number", "value": 1 } );
  mydata.push( { "name": "page_size", "value": 10 } );
   //here comes a basic jQuery helper function to handle your ajax call.
   //of course you can replace it with any other ajax handler
   //has nothing to do with dataTables
   $.ajax({
    "dataType": 'json',
    "type": "POST",
    "url": sSource,
    "data": mydata,
    "success": fnCallback
  });
}
});

Here the usual aoData is totally overwritten with the custom array mydata. This will be posted to your server:

 page_number    1
 page_size  10

Now you just need to write a function for the success handler which maps the server response to the expected variables (iTotalRecords = total_count, aaData = users etc) and then call fnCallback to process everything.

Of course this needs some fiddling around to find out how to fill out the expected server response. But this should work.

I can't produce a Fiddle or a Plunker because of ajax.

Be aware that this would only make sense if you can't change the server sided script. Also note, that in the same way i create a new array mydata, you can also push extra post data to aoData and just ignore the additional parameters on your server side.

BTW: Returning total_page_count is kinda useless since this can be computed on the client side pretty easily.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.