Push variable with HTML-Table to Datatables
Push variable with HTML-Table to Datatables
pekabo
Posts: 7Questions: 2Answers: 1
Hi @ all,
I read a csv file and generate a table in a variable (see script). How do I push the "html" variable to datatable to get this result DataTables.
$.get('stock.csv', function(data) {
// start the table
var html = '<table id="example" class="table table-striped table-bordered dt-responsive nowrap" cellspacing="0" width="100%">';
// split into lines
var rows = data.split("\n");
// parse lines
j = 0;
rows.forEach( function getvalues(ourrow) {
if(j == 0)
{
// start a table row
html += "<thead><tr>";
// split line into columns
var columns = ourrow.split(",");
var arrayLength = columns.length;
for (var i = 0; i < arrayLength; i++) {
html += "<th>" + columns[i] + "</th>";
}
// close row
html += "</tr></thead>";
j++;
}
else
{
// start a table row
html += "<tr>";
// split line into columns
var columns = ourrow.split(",");
var arrayLength = columns.length;
for (var i = 0; i < arrayLength; i++) {
if (i == 0) {
html += "<th>" + columns[i] + "</th>";
}
else{
html += "<td>" + columns[i] + "</td>";
}
}
// close row
html += "</tr>";
}
})
// close table
html += "</table>";
// insert into div
$('#container').append(html);
});
This question has an accepted answers - jump to answer
This discussion has been closed.
Answers
Seems like your code doesn't generate a TBODY tag. Probably that's causing problems.
Further, you need to include the JavaScript and css files referenced below the example you're referencing and initialize the datatable.
Hi F12Magic,
I changed the code, with the same effect. I thinks my Problem is the timing. See picture the CSV "stock.csv" loads to late.
I thought, i could first render everythink to a Variable and then push this to Variable to dataTables.
Try loading all css and scripts files in the head tag.
Then just before closing the body tag you insert your own javascript to generate the table. After you append the table to the div, you initialize the datatable.
Example:
I think this is the key bit.
Allan
Thank you F12Magic,
appreciate your help! I post your Solution for my problem, may it help some order user.
above the "how do I Load a CSV in Datatables." solved question
From here I have a continuing question. As you can see I add a the tfooter. How I can get the Sum of each Column in the footer. Till now no luck with the sum plugin or footercallback
I wish all readers merry Christmas 2016
I cant find the right place where to intergrate follwing code in my code (see above) to get sum of all columns.
Just place the footerCallback code inside the dataTables initialisation. Like:
Or take a look at the footer callback example on this site.
Or an example on codepen made by me.