I have a .CSV file with headings and data laid out as:
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.
// 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;
}