When I add success function DataTable not fill up automaticly rows in table. When I remove success function everything is all right and datatable fill correctly data in table. I want to catch in response using getAccessMessageWithStatus message by status but if I do it like this datatable no filling rows. How I can do that?
$('#' + datatableName).DataTable({
destroy: true,
'bProcessing': false,
'bServerSide': true,
'ajax': {
'url': URL,
'data': filters,
beforeSend: function() {
loader.popup('show');
},
success: function(response) {
getAccessMessageWithStatus(response);
},
complete: function() {
$listContainer.show();
$containerChoiseColumnsFilter.show();
$(".containerRaportButtons").show();
getLastSearches();
getUses();
loader.popup('hide');
}
},
'sServerMethod': "POST",
'columns': columns,
'order': order,
'responsive': true
});
Answers:
success: function(response) {
getAccessMessageWithStatus(response);
},
Or:
"dataSrc": function(response) {
if (response.status == false) {
alert(response.msg);
return [];
}
return response.aaData;
},
What is AJAX success? AJAX success is a global event. Global events are triggered on the document to call any handlers who may be listening. The ajaxSuccess event is only called if the request is successful. It is essentially a type function that's called when a request proceeds.
DataTables has the ability to read data from virtually any JSON data source that can be obtained by Ajax. This can be done, in its most simple form, by setting the ajax option to the address of the JSON data source.
Description. The partner of the drawCallback callback, this function is called at the very start of each table draw. It can therefore be used to update or clean the display before each draw (for example removing events), and additionally can be used to cancel the draw by returning false .
Remove ";" after the function name into the code.
success: function (response) {
getAccessMessageWithStatus(response)
},
There is an event from DatatTable called 'xhr.dt'. You can use it in that way.
$('#' + datatableName).on('xhr.dt', function(e, settings, json, xhr){
getAccessMessageWithStatus(json);
}).DataTable({
destroy: true,
'bProcessing': false,
'bServerSide': true,
'ajax':
{
'url': URL,
'data': filters,
beforeSend: function () {
loader.popup('show');
},
complete: function () {
$listContainer.show();
$containerChoiseColumnsFilter.show();
$(".containerRaportButtons").show();
getLastSearches();
getUses();
loader.popup('hide');
}
}
});
You shouldn't use success from ajax attribute because you will overwrite the success function from DataTable. See this piece of code from query.dataTables.js
"success": function (json) {
var error = json.error || json.sError;
if ( error ) {
_fnLog( oSettings, 0, error );
}
oSettings.json = json;
callback( json );
}
You can notice that they have a callback inside this function. This callback triggers the function _fnCallbackFire and this call the event xhr.dt
For more information, go to this page https://datatables.net/reference/event/xhr
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With