How do I change the ajax parameters?
How do I change the ajax parameters?
I'm trying to change the parameters of the ajax() object, but it won't show an updated table. This is what I do:
mtable = $('#messagestable')
.DataTable({
ajax: {
url: 'myfunction.php',
data : { a: 1, b: 2 }
}
})
Now I want to use the same url, just with changed parameters, e.g. { a: 1, b: 3 }. With mtable.ajax.params({ a: 1, b: 3 }), I seem to be able to change the parameters, because mtable.ajax.params() gives me the new ones. But when I run mtable.ajax.reload(), nothing changes and mtable.ajax.params() gives me the old parameters.
Do I make a mistake, or is this simply not working?
Replies
Hi,
As the
ajax.params()
documentation notes, it can be used to get parameters only - not set them.To set them what you need to do is use
ajax.data
as a function. That way the function is executed every time an Ajax call is made and the values can be resolved by your function.Allan
Ok, thanks, that clarifies it.
But then I just have to do table.ajax.reload()? Or which command do I need to get it working?
It doesn't work.... I took this example:
$('#example').dataTable( {
"ajax": {
"url": "data.json",
"data": function ( d ) {
d.extra_search = $('#extra').val();
}
}
} );
but that throws a 'Cannot reinitialise DataTable' error...
That is correct - you have you put your
ajax
initialisation in with your first DataTables initialisation. See this section of the manual.Allan
I'm not sure the outcome of this case... I'm having a similar problem trying to change the "data" param of the ajax object...
$("#customer_history00000000-0000-0000-0000-000000000000-table").DataTable({retrieve: true, "ajax": {
"url": "/Data/Grids",
"type": "POST",
"data": function ( d ) {
d.TableName = "s_signal_history";
d.CustomerId = 'dsf';
d.FilterField = 'fdf';
}}}).ajax.reload();
or
$("#customer_history00000000-0000-0000-0000-000000000000-table").DataTable({retrieve: true}).ajax.data = function ( d ) {
d.TableName = "s_signal_history";
d.CustomerId = 'dsf';
d.FilterField = 'fdf';
};
$("#customer_history00000000-0000-0000-0000-000000000000-table").DataTable({retrieve: true}).ajax.reload();
_
I'm not sure how to change the data function after the object has been created!