Table data loads, briefly and unformatted, before jquery DataTables loads.
Table data loads, briefly and unformatted, before jquery DataTables loads.
rimshot609
Posts: 9Questions: 6Answers: 0
Table data loads, briefly and unformatted, before jquery DataTables loads. I have researched and found many say the solution is to hide the table with css and then show it in jquery with initComplete. I have tried the following but nothing seems to work:
css:
#tblAccount {
visibility:hidden;
}
#tblCustomer {
visibility: hidden;
}
jquery:
$(function () {
$("[id*=tblAccount], [id *= tblCustomer]").prepend($("<thead></thead>").append($(this).find("tr:first"))).DataTable({
"paging": true,
"lengthChange": true,
"searching": true,
"ordering": true,
"info": true,
"autoWidth": true,
"responsive": true,
"dom": 'lBfrtip',
"buttons": ['excel', 'print', 'pdfHtml5'],
"initComplete": function () {
$('#tblAccount').show().css({ visibility: "visible" });
$('#tblCustomer').show().css({ visibility: "visible" });
}
});
})
This discussion has been closed.
Answers
See if this example helps:
http://live.datatables.net/xaqavaxe/1/edit
It uses settimeout to simulate the delay in loading the table and Datatables initialization.
You will likely need to use
columns.adjust()
to have Datatables recalculate and adjust the columns widths after showing the table.Kevin
Did this problem get solved? I would love to see the solution; I've been stuck trying to figure this out for days.
@jamiedewitz I posted an example. Does that not help? If not then please provide a simple test case showing what you are doing so we can help.
Kevin
I used the example, but my datatable still shows up with all of the columns way outside of the table, then it disappears, then re-appears again, but this time it is fully loaded and showing up correctly. I'm using razor to display the form data which is coming from a ViewModel in my C#.NET MVC application.
Before:
After:
This will be difficult to diagnose without actually seeing it. Can you post a link to your page?
Is the Datatable initially hidden when it is initialized?
If yes, then you will want to use
responsive.recalc()
along withcolumns.adjust()
when you make the table visible.If no, then there is a styling issue that is causing the Datatable to be outside the container you want it in. Impossible to say without seeing your page.
You are using
settimeout
which was in my example just to simulate the delay in loading the table. You don't want to use this in production. Without seeing your page or a test case to see the flow of your script it would be hard to say what the problem is.Kevin
Ok I will try to get it working on jsfiddle.net. My application is internal only, so I can't post a link to it.
I'm not sure that fiddle is the right place for this because it is a giant amount of data, but I didn't really know where else to go. But it does work the same way as my internal application. https://jsfiddle.net/jamiedewitz/spfx8tLz/3/
The css and script bundles I included in the layout are:
For some reason the fiddle doesn't seem to load for me. I can open other fiddles. The test case just needs to replicate the script flow. The data doesn't really matter as its not the issue. Maybe you can reduce it done to the bare minimum to replicate the issue.
Kevin
Ok, I will update it. Thanks for the help
This one is faster, but you can see it blink quickly, which is what it is doing with the table only much slower when there are 1,239 results. In this example there are only 10 results which makes it faster, but still has that initial blink which I would like to get resolved.
https://jsfiddle.net/jamiedewitz/spfx8tLz/5/
This is what I get when using the DataTables debugger btw:
Data source: DOM
Processing mode: Client-side
Draws: 1
Columns: 14
Rows - total: 1239
Rows - after search: 1239
Display start: 0
Display length: 10
Its still slow Anyway here are the changes I made:
Added the ID
DT-div
to thediv
for thetable
. And gave it thestyle="display:none;"
:Then added this code to
initComplete
. Uses jQuery show() to display thediv
. Then usescolumns.adjust()
andresponsive.recalc()
to recalc the column widths. Datatables can't calc the column widths when its initialized in a hidden element.Here is the updated fiddle:
https://jsfiddle.net/k9pwb715/
HTH,
Kevin
That is amazing Kevin! Thank you so much!