jQuery DataTables: How to show loading indicator during table reload
Example for article at https://www.gyrocode.com/articles/jquery-datatables-how-to-show-loading-indicator-during-table-reload/
by TheNerdy97
HTML
<link rel="stylesheet" href="https://cdn.datatables.net/v/dt/dt-1.10.15/datatables.min.css">
<script src="https://cdn.datatables.net/v/dt/dt-1.10.15/datatables.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-mockjax/1.6.2/jquery.mockjax.min.js"></script>
<h3>jQuery DataTables</h3>
<a href="https://www.gyrocode.com/articles/tag/jquery-datatables/">See more articles about jQuery DataTables</a> on <a href="https://www.gyrocode.com/articles/">Gyrocode.com</a><hr>
<p>
<button type="button" id="btn-reload">Reload</button>
</p>
<table id="example" class="display" cellspacing="0" width="100%">
<thead>
<tr>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th>Extn.</th>
<th>Start date</th>
<th>Salary</th>
</tr>
</thead>
<tfoot>
<tr>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th>Extn.</th>
<th>Start date</th>
<th>Salary</th>
</tr>
</tfoot>
</table>
<hr><a href="https://www.gyrocode.com/articles/tag/jquery-datatables/">See more articles about jQuery DataTables</a> on <a href="https://www.gyrocode.com/articles/">Gyrocode.com</a>
<script>
// FOR DEMONSTRATION ONLY: AJAX emulation
$.mockjax({
url: '/test/0',
responseTime: 1000,
response: function(settings){
console.log("Request data: ", settings.data);
this.responseText = {
data: [],
};
for(var i = 0; i < 100; i++){
this.responseText.data.push([
"A" + (i + 1),
"B" + (i + 1),
"C" + (i + 1),
"D" + (i + 1),
"E" + (i + 1),
"F" + (i + 1)
]);
}
}
});
</script>
JavaScript
$(document).ready(function (){
var table = $('#example').DataTable({
ajax: '/test/0',
'processing': true,
'language': {
'processing': 'Loading...'
}
});
$('#btn-reload').on('click', function(){
table.ajax.reload();
});
});