I am having a little trouble with some Javascript that I have written. The purpose of the code is the following:
- Read list of SKUs from a provided .TXT file
- Split the data at each line
- For each object make a lookup on a provided JSON api to get information about the SKU
- Output this information into a HTML table.
Currently I have this working as I would have expected however, it seems that it not blocks any other Javascript that I try to run after the for
loop.
Here is an example of my code
<script type="text/javascript">
//Set API Address
var api = "/api/AthenaService.svc/GetProductBySku/";
//Get Array of SKUs from TXT file
$.get('/Views/Locale/promoPages/LandingPages/TradeList/TradeList.txt',function(data){
//Split File into lines
var line = data.split('\n');
for(i=0;i<line.length;i++)
{
$.getJSON(api + line[i] , function(data1) {
// Request complete, NOW we can use the data we got!
$('.tListBody').append('<tr><td>' + data1.Title + '</td><td align="center">' + data1.Platform.Button + '</td></tr>');
});
};
});
$(document).ready(function() {
$('#searchLoading').fadeOut('slow');
$('#showForm').fadeIn('slow');
$('input#tradeSearch').blur(function() {
$('input#tradeSearch').quicksearch('table#Searchable tbody tr');
});
});
</script>
The problem I have is that none of the stuff in within document ready seems to work at all.
I have updated the code above to reflect the suggested fixed from below. It seems that code is running fine however my quicksearch jQuery plugin does not seem to be firing. I am wondering if this is related to the fact that the TR elements that it should be searching are newly created DOM elements?
Any help would be greatly appreciated.
Update:
The problem has been solved! A little reading through the documentation of the Quicksearch.js plugin and I figured out that it is possible to add entries into the quick search cache manually as part of my loop. This has fixed the problem.
Updated code below;
$(document).ready(function () {
var api = "/api/AthenaService.svc/GetProductBySku/";
//Get Array of SKUs from TXT file
$.get('/Views/Locale/promoPages/LandingPages/TradeList/TradeList.txt', function (data) {
//Split File into lines
var line = data.split('\n');
var qs = $('input#tradeSearch').quicksearch('.TheList tbody tr');
for (i = 0; i < line.length; i++) {
$.getJSON(api + line[i], function (data1) {
// Request complete, NOW we can use the data we got!
$('.tListBody').append('<tr><td>' + data1.Title + '</td><td align="center">' + data1.Platform.Button + '</td></tr>');
qs.cache();
});
};
});
});
Thanks for the help and suggestions all