How to get filtering options for jquery datatable (datatables.net)?

I am using jquery DataTables (from datatables.net) with server side processing and ColumnFiltering add-in. I need to add a callback that will calculate subtotals based on filtered data. For this, I want to make a separate ajax call. How can I extract the current ajax options?

+4
source share
4 answers

Assign a datatable to create var at creation, for example:

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

Later use this:

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

it returns all ajax parameters that will be sent in a normal data load request for data. Make your ajax call like this:

 $.post("url",$.param(params),function(response){....}); 
+8
source

If you are using DataTables 1.10 (current version with this answer), it is now much easier to access using 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 ); } ); 
+5
source

Depending on your question, it looks like you also need ajax ordering options. 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 answer - helped me with my desk! It was hard for me to find other parameters, so I thought I would share it here.

0
source

Try it:

 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); }); 
0
source

Source: https://habr.com/ru/post/1488048/


All Articles