I've read Loading Message using Datatables
DataTables 1.10.16 using ajax source data and server side mode.
My table has the following initialisation code:
var substancesTable = $('#substancesTable').DataTable({
"processing": true,
"serverSide": true,
"searching": false,
"ajax": function(data, callback){
// code for ajax request
},
"language": {
"lengthMenu": "_MENU_ per page",
"zeroRecords": "Sorry no records found",
"info": "Showing <b>_START_ to _END_</b> (of _TOTAL_)",
"infoFiltered": "",
"infoEmpty": "No records found",
"processing": '<i class="fa fa-spinner fa-spin fa-2x fa-fw"></i>'
},
});
DataTables correctly uses the "processing"
property - it shows a FontAwesome spinner (.fa-spinner
) when the data is ready to be rendered by DataTables; which happens when the ajax request is complete.
However, I want to show a message - such as "Loading data..." - whilst the ajax request is in process.
So the advice on the previous SO link says about using the loadingRecords
property within language
. So I added this:
"language:" {
// ...
"loadingRecords": "Loading data..."
}
This does nothing.
Furthermore, I would prefer to use something similar to my overlay which I set using the processing
property. I believe that using loadingRecords
only adds a row to the table whilst the ajax process is being completed, which isn't ideal anyway.
I can't see anything in DataTables documentation about this.
What options do I have here? How do I inform the user that the ajax request is in process? This happens quite a lot as some searches take >4 seconds in my application due to the nature of the data being searched.
There is conflicting (and wrong) information on the DataTables website: https://datatables.net/forums/discussion/41654/how-to-display-a-progress-indicator-for-serverside-processing says that processing
property can be used for this question. But https://datatables.net/reference/option/language.processing (correctly) says that this is for "when the table is processing a user action".
In my experience processing
only fires when DataTables is doing some client-side work (i.e. updating the table), nothing to do with waiting for server side data.
DataTables' server-side processing mode is a feature that naturally fits with Scroller. Server-side processing can be used to show large data sets, with the server being used to do the data processing, and Scroller optimising the display of the data in a scrolling viewport.
Load(IDataReader, LoadOption) Fills a DataTable with values from a data source using the supplied IDataReader. If the DataTable already contains rows, the incoming data from the data source is merged with the existing rows according to the value of the loadOption parameter.
The best alternative is jQuery Dynatable. It's not free, so if you're looking for a free alternative, you could try List. js or Webix DataTable. Other great apps like DataTables are Frappe DataTable, Dash DataTable, wpDataTables and ag-Grid.
This option allows DataTables to create the nodes (rows and cells in the table body) only when they are needed for a draw.
According to the language.loadingRecords
reference: (formatted for clarity)
Note that this parameter is not used when loading data by server-side processing, just Ajax sourced data with client-side processing.
So in your case — using server-side processing, the loading message/indicator will never appear.
However, it's actually a simple table row (tr
) element which DataTables adds to the table body (tbody
), so you can manually add the tr
from your ajax
option/function.
Here's an example where I used jQuery.ajax()
to make the AJAX request, and the beforeSend
option to display the loading message:
Demo on CodePen
$(document).ready(function() {
$('#example').DataTable( {
serverSide: true,
processing: true,
language: {
processing: '<i class="fa fa-spinner fa-spin fa-2x fa-fw"></i>'
},
ajax: function(data, callback){
$.ajax({
url: 'http://example.com/path/to/some-file',
'data': data,
dataType: 'json',
beforeSend: function(){
// Here, manually add the loading message.
$('#example > tbody').html(
'<tr class="odd">' +
'<td valign="top" colspan="6" class="dataTables_empty">Loading…</td>' +
'</tr>'
);
},
success: function(res){
callback(res);
}
});
}
} );
} );
And if your ajax
option is an object (which DataTables simply passes to jQuery.ajax()
), then:
$(document).ready(function() {
$('#example').DataTable( {
serverSide: true,
processing: true,
language: {
processing: '<i class="fa fa-spinner fa-spin fa-2x fa-fw"></i>'
},
ajax: {
url: 'http://example.com/path/to/some-file',
beforeSend: function(){
// Here, manually add the loading message.
$('#example > tbody').html(
'<tr class="odd">' +
'<td valign="top" colspan="6" class="dataTables_empty">Loading…</td>' +
'</tr>'
);
}
}
} );
} );
Note: Change the example
to your table ID and the colspan
value to the appropriate one based on your table..
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