Displaying CSV Headers using PapaParse Plugin
Asked Answered
B

2

9

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!

Brinson answered 27/10, 2014 at 21:50 Comment(1)
Enabling header row support simply keys data on every row by the field name. Without header row, the data is returned as an array of arrays, so you would access values numerically. With header row, though, data is an array of objects, and the first row is consumed as the header row, not as part of the data.Dace
B
8

Well, I figured it out...

The header titles were contained within a different object. results.meta['fields']

This is how I am printing the results from papa parse.

$.each(results.meta['fields'], function(i) {
    $("#headers").append($("<td/>").text(results.meta['fields'][i]));
});
Brinson answered 27/10, 2014 at 22:17 Comment(0)
P
0

try using header:false, it'll display the header as array.

Pasco answered 22/9, 2022 at 6:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.