0

我对 jQuery 完全陌生,因此对任何明显的错误表示歉意。

我有一个带有标题和数据的 .CSV 文件,其布局如下:

订单号,库存状态,数量,评论,日期 1234567,有库存,15,全红,2012 年 8 月 15 日 1234568,缺货,203,离开邻居,2012 年 8 月 21 日 1234569,订购中,20 , 镀铬饰面, 17/08/2012 1234570, 其他, 140, 木制服装, 01/09/2012

我有一个带有 4 个按钮的 HTML 页面(对应于库存状态):

有货, 缺货, 订购中, 其他,

我想要做的是,当我单击上述其中一项时,我的代码会消失,搜索与库存状态列匹配的所有记录并返回订单号、数量和评论。

到目前为止的代码:

var allText =[];
var allTextLines = [];
var Lines = [];
var txtFile = new XMLHttpRequest();

txtFile.open("GET", "file://C:/data.txt", true);
txtFile.onreadystatechange = function()
{
 allText = txtFile.responseText;
 allTextLines = allText.split(/\r\n|\n/);
};

// On opening the site, show the loading icon and GIF.
$(document).ready(function () {
$("#Outline").hide();
$("#loadingTable").delay(1000).hide(0);
$.ajax({
    type: "GET",
    url: "data.txt",
    dataType: "text",
    success: function (data) { processData(data); }     
})
alert("0.2")
});

function showSLAMenus() {
$("#Outline").show();
};

$("#OutOfStock").click(function() {
alert("OutOfStock search")
// returns Order Number, Quantity and Comments for items Out of Stock
});

$("#InStock").click(function() {
alert("InStock search")
// returns Order Number, Quantity and Comments for items In Stock
});

$("#Other").click(function () {
alert("Other search")
// returns Order Number, Quantity and Comments for items Other
});

function processData(allText) {
alert("1")
var allTextLines = allText.split(/\r\n|\n/); 
var headers = allTextLines[0].split(','); 
var lines = []; 

for (var i=0; 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); 
    } 
} 
alert(lines); 
} 

我的第二次尝试:

// On opening the site, show the loading icon and GIF.
$(document).ready(function () {
$("#Outline").hide();
$("#loadingTable").delay(1000).hide(0);
var data = []; // Empty array in global scope where we will store your data

// Your ajax call to get the data and store it in the var above
$.ajax({
    type: "GET",
    url: "data.txt",
    dataType: "text",
    success: function (data) { processData(data); }
})
});

function showSLAMenus() {
$("#Outline").show();
};

setTimeout(showSLAMenus, 1001);

$("#Other").click(function () {
alert("Other1")
// An example search call
var output = searchData(data, "Other");

alert("Other2")

// Dump out the results of our search
for (var i in output) {
    $("div#output").html(output[i] + "<br>");
}
});

// Main function to process the data into an array
function processData(allText) {
var allTextLines = allText.split(/\r\n|\n/);
var headers = allTextLines[0].split(',');
var lines = [];

for (var i = 0; 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);
    }
    //alert(lines);
}
return lines; // Returns the data you need, to be stored in our variable 
}

// A search function using the jQuery inArray method
// - matches the data position in the array and returns a new array of matched data
function searchData(data, search) {
alert("searchData Called")
// Create a temp array to store the found data
var tempArray = [];

// Loop through the data to see if each array has the search term we need
for (i = 0; i < data.length; i++) {
    var pos = $.inArray(search, data[i]);

    // Add found data to the array
    if (pos !== -1) {
        tempArray.push(data[i]);
    }
}

// Return the array of matched data
return tempArray;
}

你好谢谢。您在 jsFiddle 中的代码似乎正在运行。但是,查看我的代码时,单击“其他”按钮

alert("Other1")
// An example search call
var output = searchData(data, "Other");
alert("Other2")

显示警报 Other1,但我相信它无法调用 searchData。

我还需要吗

txtFile.open("GET", "file://C:/data.txt", true);
txtFile.onreadystatechange = function()
{
   allText = txtFile.responseText;
   allTextLines = allText.split(/\r\n|\n/);
};

在我的代码中???

谢谢。

4

1 回答 1

0

你快到了。最好的办法是使用 jQuery$.inArray方法将数组中的行与您所追求的数据相匹配。您只需要一个搜索函数,它将您所追求的数据作为新数组返回。这是一个例子 -编辑:http: //jsfiddle.net/gNTWk/1/

这对您的代码有何影响?在下面添加:

$(document).ready(function () {
    
    var data = []; // Empty array in global scope where we will store your data
    
    // Your ajax call to get the data and store it in the var above
    $.ajax({
        type: "GET",
        url: "data.txt",
        dataType: "text",
        success: function(data){
            data = processData(data);
        }     
    });

    // Your main function to process the data into an array
    function processData(allText) {
        alert("1")
        var allTextLines = allText.split(/\r\n|\n/); 
        var headers = allTextLines[0].split(','); 
        var lines = []; 
    
        for (var i=0; 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); 
            } 
        } 
        return lines; // Returns the data you need, to be stored in our variable 
    }


    // A search function using the jQuery inArray method
    // - matches the data position in the array and returns a new array of matched data
    function searchData(data, search){
        // Create a temp array to store the found data
        var tempArray = [];
        
        // Loop through the data to see if each array has the search term we need
        for(i=0; i<data.length; i++){
            var pos = $.inArray(search, data[i]);
            
            // Add found data to the array
            if(pos !== -1){
                tempArray.push(data[i]);
            }
        }
        
        // Return the array of matched data
        return tempArray;
    }


    // An example search call
    var output = searchData(data, "Other");

    // EDITED OUTPUT LOOP    
    for(i=0; i<output.length; i++){
        $("div#output").append(output[i] + "<br>");
    }

 });

编辑: 抱歉searchData功能很好。只是输出循环出错了。上面的代码现在应该可以正常工作了。编辑jsfiddle:http: //jsfiddle.net/gNTWk/1/

于 2012-09-04T09:31:31.760 回答