Quantcast
Channel: Recent Discussions — DataTables forums
Viewing all articles
Browse latest Browse all 82439

Push variable with HTML-Table to Datatables

$
0
0

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);
});

Viewing all articles
Browse latest Browse all 82439

Trending Articles