jQuery DataTables - Slow initiation, "Normal" html table shown in the beginning
[edit to add: This older answer refers to the previous DataTables API. The jQueryUI options are deprecated and recommendations for replacement are found here: https://datatables.net/manual/styling/jqueryui Also, fnInitCallback (as with all other options) dropped Hungarian notation and is now initCallback]
Original answer follows:
My caveat is that I am not familiar with _Scripts partial views, so the advice below is what I would give someone just including and calling JavaScript in the 'normal' ways:
Style the plain HTML table so that it shares the same appearance as the final. If you have jQuery UI enabled (
bJQueryUI: true
), this means having the jQuery UI classes in the 'plain' table rather than waiting for DT to add them.Use various FOUC (flash of unstyled content) techniques to hide the table until it is ready to render. DataTables API has useful callbacks that you can use for the "show it now" part of things, such as fnInitCallback. The most basic (but accessibility-damaging) technique is to style the table with display:none, and in the callback, use
$('#myTable').show()
or some variation. Searching on the internet should provide some great solutions that preserve accessibility.
Other than that, it's really just a question of (as you say!) tolerance for "acceptable". We use server-side processing (returning far fewer records), a script loader for faster script loading time (we're experimenting with head.js but there are others!), and the minimized versions of the scripts. Even with this, we sometimes see the plain table for a moment before it becomes a DT, but since internet users are accustomed to seeing pages being 'built' before their eyes as elements are loaded, it was an acceptable tradeoff. For you, it might not be.
Good luck!
I had the same problem. Try this:
var dTable=$("#detailtable").dataTable({
"bProcessing":true,
"bPaginate":false,
"sScrollY":"400px",
"bRetrieve":true,
"bFilter":true,
"bJQueryUI":true,
"bAutoWidth":false,
"bInfo":true,
"fnPreDrawCallback":function(){
$("#details").hide();
$("#loading").show();
//alert("Pre Draw");
},
"fnDrawCallback":function(){
$("#details").show();
$("#loading").hide();
//alert("Draw");
},
"fnInitComplete":function(){
//alert("Complete");
}
I did a very simple solution that works fine. In the DataTable initialization I used the method show():
$(document).ready(function() {
$('#example').dataTable({
"order": [[ 0, 'asc' ]]
});
$('#example').show();
} );
... and in the HTML table I put the style display:none:
<table id="example" class="display" cellspacing="0" width="100%" style="display:none">
Good luck!
Based on @hanzolo suggestion - here's my comment as an answer (and what my dataTable looks like):
var stockableTable = $('#stockable').dataTable({
"bLengthChange": false,
"iDisplayLength": -1,
"bSort": false,
"bFilter": false,
"bServerSide": false,
"bProcessing": false,
"sScrollY": "500px",
"sScrollX": "95%",
"bScrollCollapse": true,
// The following reduces the page load time by reducing the reflows the browser is invoking
"fnPreDrawCallback":function(){
$("#loading").show();
},
"fnDrawCallback":function(){
},
"fnInitComplete":function(){
$("#details").show();
this.fnAdjustColumnSizing();
$("#loading").hide();
}
});