Hi,

I’m completely new to jQuery so apologies for any obvious errors.

I have a .CSV file with headings and data laid out as:

Order Number, Stock Status, Quantity, Comments, Date
1234567, In Stock, 15, All in Red, 15/08/2012
1234568, Out of Stock, 203, Leave with neighbor, 21/08/2012
1234569, On Order, 20, Chrome finish, 17/08/2012
1234570, Other, 140, Wooden garment, 01/09/2012
1234571, Other, 541, Chrome finish, 31/08/2012

I have a HTML page with 4 buttons (correspond with Stock Status):

In Stock, Out of Stock, On Order, Other,

What I am trying to do is when I click one of the above, my code goes away, searches for all records that match on Stock Status column and returns Order Number, Quantity and Comments.
Code so far:

// 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;
}

However, looking at my code, when clicking the Other button

$("#Other").click(function () {
alert("Other1")

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>");
}
});

Alert Other1 is displayed, but it fails to call searchData i believe.

Any help is appreciated.

Thanks

Recommended Answers

All 2 Replies

bump

so does it never get to alert("Other2")?

Try wrapping the var output = searchData(data, "Other") in a try-catch and alert the message. See if anything comes up.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.