Javascript将CSV文件加载到数组中

Aqu*_*267 8 javascript wordpress jquery file

我正在使用Wordpress开发一个网页.网页需要有一个与所有县组合的组合框.我有一个csv格式的数据集,对于所有这些县都有大约10k行.当用户在下拉列表中选择一个县时,我只想要在网页中显示所选择的县数据.这是我的要求.

在wordpress,我的网页我正在添加js文件

<script type="text/javascript" src="http://xxx/wp     content/uploads/2014/05/countyList1.js"></script>
Run Code Online (Sandbox Code Playgroud)

并且网页下拉的代码是

<select name="county" id="county" onload="setCounties();" onchange="getSelectedCountyData();"></select>
Run Code Online (Sandbox Code Playgroud)

在countyList1.js文件中,我有setCounties()和getSelectedCountyData()函数.

到目前为止,我可以看到与县列表的下拉列表.我不知道如何阅读CSV文件并将选定的县过滤器应用于此列表.

我尝试了FileReader对象,我可以在网页上加载CSV内容,但我不希望用户选择CSV.我已经有了数据集.

我正在尝试使用此SO帖子中的这个jquery.csv-0.71库如何使用javascript从*.CSV文件读取数据?但我需要帮助.

这是在选择县时调用的代码

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);
}
Run Code Online (Sandbox Code Playgroud)

小智 22

我强烈建议您查看此插件:

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

我将它用于处理大型CSV文件的项目,它可以很好地将CSV解析为数组.您也可以使用它来调用您在代码中指定的本地文件,这样您就不依赖于文件上载.

上面包含插件后,您可以使用以下方法解析CSV:

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

然后,所有内容都将存在于数组数据中,供您根据需要进行操作.如果需要,我可以提供处理数组数据的更多示例.

插件页面上有很多很好的例子可以用来做各种各样的事情.


Hal*_*yon 4

您不能使用 AJAX 从用户计算机获取文件。这绝对是错误的做法。

使用文件读取器API:

<input type="file" id="file input">
Run Code Online (Sandbox Code Playgroud)

js:

console.log(document.getElementById("file input").files); // list of File objects

var file = document.getElementById("file input").files[0];
var reader = new FileReader();
content = reader.readAsText(file);
console.log(content);
Run Code Online (Sandbox Code Playgroud)

然后解析content为 CSV。请记住,您的解析器当前不处理 CSV 中的转义值,例如:value1,value2,"value 3","value ""4"""