Javascript loading CSV file into an array

file, javascript, jquery, wordpress

Solution

I highly recommend looking into this plugin:

http://github.com/evanplaice/jquery-csv/

I used this for a project handling large CSV files and it handles parsing a CSV into an array quite well. You can use this to call a local file that you specify in your code, also, so you are not dependent on a file upload.

Once you include the plugin above, you can essentially parse the CSV using the following:

$.ajax({
    url: "pathto/filename.csv",
    async: false,
    success: function (csvd) {
        data = $.csv.toArrays(csvd);
    },
    dataType: "text",
    complete: function () {
        // call a function on complete 
    }
});

Everything will then live in the array data for you to manipulate as you need. I can provide further examples for handling the array data if you need.

There are a lot of great examples available on the plugin page to do a variety of things, too.

Problem

I am developing a web page in Wordpress. The webpage needs to have a combobox with all counties. I have a dataset in csv format which has some 10k rows for all these counties. When the user selects a county in the dropdown, I want only the selected county data displayed in the web page. This is my requirement. In wordpress, my web page I am adding the js file using ``` <script type="text/javascript" src="http://xxx/wp content/uploads/2014/05/countyList1.js"></script> ``` and the code for webpage dropdown is ``` <select name="county" id="county" onload="setCounties();" onchange="getSelectedCountyData();"></select> ``` In countyList1.js file I have the setCounties() and getSelectedCountyData() functions. So far I can see the dropdown with counties list. I don't know how to read the CSV file and apply the selected county filter to this list. I tried the FileReader object and I can load the CSV contents on the web page but I don't want the user to select the CSV. I have the dataset already. I am trying to use this jquery.csv-0.71 library from this SO post How to read data From *.CSV file using javascript? but I need help. Here's the code which gets called when a county is selected ``` function getSelectedCountyData() { cntrySel = document.getElementById('county'); //selCty = countyList[cntrySel.value]; handleFiles(); } function handleFiles() { $(document).ready(function () { $.ajax({ type: "GET", url: "D:\Docs\Desktop\csvfile.csv", dataType: "csv", success: function (data) { processData(data); } }); }); } function processData(allText) { var allTextLines = allText.split(/\r\n|\n/); var headers = allTextLines[0].split(','); var lines = []; for (var i = 1; i < allTextLines.length; i++) { var data = allTextLines[i].split(','); if (data.length == headers.length) { var tarr = []; for (var j = 0; j < headers.length; j++) { tarr.push(headers[j] + ":" + data[j]); } lines.push(tarr); } } console.log(lines); drawOutput(lines); } function drawOutput(lines) { //Clear previous data document.getElementById("output").innerHTML = ""; var table = document.createElement("table"); for (var i = 0; i < lines.length; i++) { var row = table.insertRow(-1); for (var j = 0; j < lines[i].length; j++) { var firstNameCell = row.insertCell(-1); firstNameCell.appendChild(document.createTextNode(lines[i][j])); } } document.getElementById("output").appendChild(table); } ```

Original source

Related problems