2

I have a simple csv file. Sample data is given below (sample.csv).

date,team_1,team_2
2019-06-12,AUS,PAK
2019-06-13,IND,NZ

I want the above to be displayed as a table in a HTML page, such that new rows will automatically add to the table as and when a new record is added to the csv file.

Can someone please help me with a very simple solution?

EDIT: Based on answer to this question, I have written (copied) the following piece of code, but other than the first line, it does not show anything.

function createTable() {
  var array = [
    ["date","team_1","team_2"],
    ["2019-06-12","AUS","PAK"],
    ["2019-06-13","IND","NZ"]
  ];
  var content = "";
  array.forEach(function(row) {
    content += "<tr>";
    row.forEach(function(cell) {
      content += "<td>" + cell + "</td>";
    });
    content += "</tr>";
  });
  document.getElementById("t1").innerHTML = content;
}
createTable()
<table id="t1"> </table>

6
  • Thanks @mplungjan! I have very limited knowledge of HTML and JS, and I have looked at more or less all the questions and top answers retrieved from Google search on this topic. However, I was not able to use any of those "effectively" to solve my problem, probably due to my lack of knowledge of this technology. Hence, I would really appreciate some "concrete" help, if possible. Commented Jun 13, 2019 at 15:06
  • With regards to the SO question marked as duplicate, I had seen that. I have 2 questions - (1) How do I make createTable() function read a CSV file instead of a hardcoded array, and (2) How do I invoke that function from <table> so that the rows and cells are populated based on the content. Commented Jun 13, 2019 at 15:13
  • 1
    See my update - I made you a snippet. and reopened the question Commented Jun 13, 2019 at 15:19
  • SO prefers ONE question at a time Commented Jun 13, 2019 at 15:19
  • So to get the data, you would need to split on \n and then split on comma Commented Jun 13, 2019 at 15:21

1 Answer 1

1

Here this code works correctly. Open a file.csv as you described. When you add or delete line in the CSV it works accordingly in the HTML page.

var init;
    const logFileText = async file => {
        const response = await fetch(file);
        const text = await response.text();
        all = text.split('\n');
        if (init !== all.length) {
            //console.log(all.length, init);
            init = all.length;
            //console.log('changed');
            var arr=[];
            all.forEach(el => {
                el=el.split(',');
                arr.push(el);
            });
            // console.log(arr);

            createTable(arr);

        }

    }

    function createTable(array) {
        var content = "";
        array.forEach(function (row) {
            content += "<tr>";
            row.forEach(function (cell) {
                content += "<td>" + cell + "</td>";
            });
            content += "</tr>";
        });
        document.getElementById("t1").innerHTML = content;
    }

    var file = 'file.csv';
    logFileText(file);
    setInterval(async () => {
        await logFileText(file);
    }, 2000);
<table id="t1"> </table>
Sign up to request clarification or add additional context in comments.

1 Comment

It is an interesting problem I have solved. I feel pleasure.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.