7

I am using the PapaParse plugin for csv files. I have this function below that creates a table to display the CSV results.

function handleFileSelect(evt) {
var file = evt.target.files[0];

Papa.parse(file, {
  header: true,
  dynamicTyping: true,
  complete: function(results) {

    $.each(results.data, function(i, el) {
        var row = $("<tr/>");
        row.append($("<td/>").text(i));
        $.each(el, function(j, cell) {
                row.append($("<td/>").text(cell));
        });
        $("#results tbody").append(row);
    });


  }
  });

}

Even with header:true set, I can not seem to get the headers to show up in the table but the rest displays perfectly.

And to be honest, I found this script online and am having trouble even understanding how it is working.

Any ideas? Thank you in advance!

4

1 回答 1

6

嗯,我想通了...

标题标题包含在不同的对象中。results.meta['fields']

这就是我打印 papa 解析结果的方式。

$.each(results.meta['fields'], function(i) {
    $("#headers").append($("<td/>").text(results.meta['fields'][i]));
});
于 2014-10-27T22:17:48.603 回答