Answer by Chris Petersn for How to get an html table out of a plain text table

This works for your data:

function readTextFile(file)
{
    var rawFile = new XMLHttpRequest();
    var allText = '';
    rawFile.open('GET', file, false);
    rawFile.onreadystatechange = function ()
    {
        if(rawFile.readyState === 4)
        {
            if(rawFile.status === 200 || rawFile.status == 0)
            {
                allText = rawFile.responseText;
            }
        }
    }
    rawFile.send(null);
    return allText;
}

function returnColumns(file)
{
    var allText = readTextFile(file);
    var lines = allText.split('\n');
    var data = [];
    for(var i = 0; i < lines.length; i++)
    {
        if (lines[i] != '')
        {
            data[i] = [lines[i].slice(0, 9), lines[i].slice(10, 16), lines[i].slice(17, 25), lines[i].slice(26) ];
        }
    }
    return data;
}

function displayHtml()
{
    var data = returnColumns('data.txt');
    var html = '<table border="3">';
    for (var r = 0; r < data.length; r++)
    {
        html += '<tr>';
        for (var c = 0; c < data[r].length; c++)
        {
            html += '<td>' + data[r][c] + '</td>';
        }
        html += '</tr>';
    }
    html += '</table>';
    document.getElementById('content').innerHTML = html;
}

Monday 21st August 2017 7:54 am

Back to User Chris Petersn - Stack Overflow blog