4

I'm using jquery DataTables (from datatables.net) with server-side processing and ColumnFiltering add-on. I need to add a callback which will compute subtotals based on filtered data. In order to achieve this I want to make a separate ajax call. How can I extract the current ajax parameters?

madth3
  • 7,275
  • 12
  • 50
  • 74
Alex
  • 387
  • 4
  • 16

4 Answers4

9

Assign datatable object to a var on creation, for example:

var oTable = $("selector").dataTable({...});`

Later use this:

var params = oTable.oApi._fnAjaxParameters(oTable.dataTable().fnSettings());

it returns all ajax parameters which would be sent in a normal data loading request for datatables. Make your ajax call like this :

$.post("url",$.param(params),function(response){....});
madth3
  • 7,275
  • 12
  • 50
  • 74
Alex
  • 387
  • 4
  • 16
5

If you're using DataTables 1.10 (the current version as of this answer), this is now a lot easier to access with the ajax.params() method.

Example from http://datatables.net/reference/api/ajax.params()

var table = $('#example').DataTable( {
    ajax: "data.json",
    serverSide: true
} );

table.on( 'xhr', function () {
    var data = table.ajax.params();
    alert( 'Search term was: '+data.search.value );
} );
MrDerp
  • 1,014
  • 2
  • 12
  • 17
0

Based on your question it looks like you might want the ajax ordering parameters as well. The code below assumes you have a datatable called 'your_table'.

$.ajax({
   url: "your_url",
   data: {         
       orderColumn: your_table.ajax.params().order[0]['column'],
       orderDirection: your_table.ajax.params().order[0]['dir'],
       searchText: your_table.ajax.params().search.value
   }
});

Thank you MrDerp for your response - helped me with my own table! I had difficulty finding the other parameters so I thought I would share here.

soroxis
  • 159
  • 2
  • 6
0

Try this:

table.on( 'xhr', function () {
            var data = table.ajax.params();
            var filter_values = [];

            //suppose you have 10 columns in your datatable
            for(i=0;i<10;i++){
                search_value = "sSearch_"+i;
                filter_values.push(data[search_value])
            }
            console.log(filter_values);
        });
Gaurav Neema
  • 146
  • 1
  • 1
  • 12