I am using DataTables 1.10
Does anyone know how to dynamically add a parameter to the ajax call before table.draw() so my request has new parameters? I've looked everywhere and cannot find an answer.
I have buttons that a person can press and based on that button send different parameters to the server.
$('#mytable').DataTable({
iDisplayLength: 10,
responsive: true,
processing: true,
serverSide: true,
searching: false,
bLengthChange: false,
bProcessing: true,
paging: true,
ajax: {
url: me.url,
dataType: 'json',
cache:false,
type: 'GET',
data: function ( d ) {
$.extend( d, me.data);
d.supersearch = $('.my-filter').val();
}
},
columns: me.columns,
columnDefs: me.renderer,
initComplete: function() {
}
});
This all works fine but then I try to tie it to a button to pass new parameters.
$('.button').on('click', function(){
var table = $('#mytable').DataTable();
table.ajax.params({name: 'test'}); <- I want to do something like this
table.draw();
})
I have modified your code to do what you need.
Initialize a data table:
Handle click event on a button:
NOTE: I'm assuming that this is the only place where you would be adding dynamic parameters for AJAX call.
My way is not so pretty, but very simple and works great: when I want to pass custom parameters while redrawing the DataTable, first I simply change its URL:
Then, if necessary, as a first step of the "on draw.dt" event I change it back to the normal URL:
The only disadvantage of this approach that it needs some trick to squeeze those custom parameters into the alternative path. Besides, it needs some server-side work also to prepare that alternative route and to extract those custom parameters from it.