Loading datatables form ajax request
Loading datatables form ajax request
Hi,
I'm using datatables and am trying to load in approximately 2500 rows of data. The request for getting the data from the API only takes about 2 seconds, however in Chrome and Safari the table loads quite long, around 25 seconds. Also, in Chrome it tends to 'hang' for around 5 seconds after the loading is finished (I have a simple custom loading indicator).
My code:
var dataSet = $.ajax({
url: "sales/data",
type: "GET"
}).done(function() {
var table = $('#example').DataTable( {
data: dataSet.responseJSON,
columns: [
{ title: "id"},
{ title: "First Name" },
{ title: "Last Name" },
{ title: "Phone"},
{ title: "Postcode" },
{ title: "Created at"},
{ title: "Membership"},
{ title: "Contact"},
{ title: "Comment"},
{ title: "By"} ,
{ title: "Ticket"},
{ title: "Details"}
],
order: [ 5, 'desc' ],
columnDefs: [ {
"targets": 10,
"data": null,
"defaultContent": "<button id='create-ticket'>Ticket</button>"
},
{
"targets": 11,
"data": null,
"defaultContent": "<button id='family-
details'>Details</button>"
}],
bSortClasses: false,
pageLength: 50,
dom: 'Bfrtip',
buttons: [
'csv'
],
fnInitComplete: function() {
$('.loader').hide();
$('#data-table').show();
}
});
This question has an accepted answers - jump to answer
Answers
Use
deferRender
.If that doesn't resolve it, please link to a test case showing the issue.
Thanks,
Allan
Thanks for the fast response. This doesn't solve it unfortunately. The weird thing is that is appears to be a lot less on the live environment. On the local server it loads and shows the table, however it 'freezes' the browser. If I check my activity monitor one of the Chrome helpers shows it's using 98% of my cpu. Once the scrolling works everything is fine and it works as expected.
I'd really need a link to the page to be able to profile it and understand where the time sink is.
Allan
Ok, I guess it has to do with the type of data I'm passing to it, if I populate the table with some random number it loads almost instantly, even with 50k rows. Are there performance issues with longer strings? Most are not very long, maybe 170 characters.
Not as far as I am aware - but that isn't to say that there absolutely isn't.
Allan