Papaparse/vfile doesnt work- Garbled output

2019-09-16 16:49发布

问题:

I posted a question regarding parsing large csv files Jquery crashes while parsing large csv file. It involves reading a csv file and tablifying it. I tried using the code given in one of the responses but it doesn't work..

Here's my entire code:

<!DOCTYPE html>
 <meta http-equiv="content-type" content="text/html;charset=utf-8" />
 <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script src="PapaParse-4.1.0/papaparse.js"></script>
<script src="virtual-list-master/vlist.js"></script>
<script>
$("#fUpload").bind("change", function(evt) {
    var bigFile = evt.target.files[0];
    var rows = [];

    Papa.parse(bigFile, {
        delimiter: ",",
        newline: "\n",
        header: false,
        dynamicTyping: false,
        worker: false,
        chunk: function(results) {
            rows = rows.concat(results.data);
        },
        complete: function() {
            var list = new VirtualList({
              h: 300,
              itemHeight: 30,
              totalRows: rows.length - 1,
              generatorFn: function(row) {
                  var el = document.createElement("tr");
                  var html = '';
                  html += '<td>' + row + '</td>';
                  for(var j = 0; j < rows[row].length; j++) {
                      html += '<td>' + rows[row][j] + '</td>';
                  }
                  el.innerHTML = html;
                  return el;
              }
            });

            document.getElementById('table').appendChild(list.container)
        }
    });
});
</script>

<input type="file" id="fUpload" />
<table style="width: 100%">
    <tbody id="table">
    </tbody>
</table>

I have Papaparse-4.1.0 and virtual-list-master folders within my current working directory. But when I open this in a browser and upload a csv file, no table is printed below. There seems to be no defects in the upload function since the answerer showed a fiddle demo of the same; I'm just reusing it here. You can see the fiddle here:http://jsfiddle.net/8e99j5v9/5/

Can someone please tell me why my code doesn't work?

EDIT I thank Sergiu for proposing a working solution but the resultant table that is delivered is thoroughly garbled.. Rows and columns are overlapping like this

Can someone help?

回答1:

JavaScript code is interpreted/executed in the order it appears in the HTML structure. It will also only "have access" to HTML before it. So $("#fUpload") will try and find an element with id #fUpload in body, but taking into account what I said before (the order in which stuff is "loaded/executed") it won't find anything, because <input type="file" id="fUpload" /> is after this script tag.

Solutions: 1) Move the <script> tags in a head sectionand the input and table in abodysection and move the$("#fUpload").bind(...code in a$(document).ready` callback. (Read about it here)

2) Move the input and table above the script tags.

Why will 1 work? Because of $(document).ready(.... This tells the code inside of it to execute after the whole HTML (including the input) was loaded.