I obtain table data from database via AJAX request. And I need to change data parameter in AJAX request and refresh the table.
I am refreshing table with command
$('#table1').DataTable().ajax.reload();
I have the following code
$('#table1').DataTable({
/* SERVER SIDE PROCESSING */
"serverSide": true,
"ajax":
{
"url": "Home/Search",
"type": "POST",
"data": {
'searchType': GetSearchType(),
'searchText': GetSearchText()
//'searchType': $.mynamespace.searchType
//'searchText': $.mynamespace.searchText
//'searchType': localStorage.getItem("searchType"),
//'searchText': localStorage.getItem("searchText"),
}
}
});
But after AJAX reload, original request to the server is sent and new parameter values are ignored. I tried pass the data to the request via function, global variable and browser storage but none of the approach work. On the internet I find solution with
aoData.push()
function but I don't know how to use it.
My version of jQuery DataTables is 1.10.7.
I also tried destroying and recreating the table with this code:
$('#table1').DataTable({
"ajax":
{
"url": "Home/Search",
"type": "POST",
"data": {
'searchType': GetSearchType(),
'searchText': GetSearchText()
}
},
"destroy" : true
}).ajax.reload();
but I am getting error message:
DataTables warning: table id=table1 - Ajax error (http://www.datatables.net/manual/tech-notes/7)
The parameters dictionary contains a null entry for parameter 'draw' of non-nullable type 'System.Int32'
Okay I found the solution, in the reinitializing of the table, it is needed to specify all settings again otherwise they are taken from default. so the final code is
but if someone will find better solution, please share it.
This is how i accomplish it:
var onSearchClick = function () { search(); };
You can use function as a value for
ajax.data
option as shown below.That way your code will be run every time the client makes request to the server and not once as with your initial code.
Then use
$('#table1').DataTable().ajax.reload()
when you need to reload the table or$('#table1').DataTable().ajax.reload(null, false)
if you don't want to reset the current page. Seeajax.reload()
for more information.