0

I am having a little trouble with some Javascript that I have written. The purpose of the code is the following:

  1. Read list of SKUs from a provided .TXT file
  2. Split the data at each line
  3. For each object make a lookup on a provided JSON api to get information about the SKU
  4. 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

  • What are searchLoading and showForm? Are those classes, IDs? Also the slow should have quotes around it. – frostyterrier Apr 25 '13 at 09:03
  • check your browser console to see whether there is any error – Arun P Johny Apr 25 '13 at 09:04
  • @frostyterrier double quotes are not the problem, but the selectors seems to be wrong – Arun P Johny Apr 25 '13 at 09:05
  • 1
    try changing `$('searchLoading').fadeOut(slow);` to `$('#searchLoading').fadeOut(slow);` assuming `searchLoading` is an element id – Arun P Johny Apr 25 '13 at 09:07
  • I never mentioned double quotes? I was just saying that I don't think fadeOut(slow) works without the quotes around the word 'slow.' I agree, the selectors seem to be wrong. – frostyterrier Apr 25 '13 at 09:15
  • Okay that did hep me out. The quicksearch still does not seem to work though. Is it possible that this is caused by the tr elements that it is searching being newly created? – Darragh Verschoyle Apr 25 '13 at 09:25
  • just to see if i understand you right, you expect that all of your `$('.tListBody').append( ... )` are done before you call the `$('input#tradeSearch').quicksearch('table#Searchable tbody tr')`? Do you want to have the rows in the same order like in the `TradeList.txt`? – t.niese Apr 25 '13 at 09:35
  • Yes that is what I am trying to achieve. I actually dont mind what order the rows appear in but I do want to be able to do the quicksearch when building the table is complete. I am using the quicksearch plugin [link](https://github.com/riklomas/quicksearch) – Darragh Verschoyle Apr 25 '13 at 10:30
  • I have never used quicksearch myself so I don't know for sure, but I just found this question which suggests it doesn't work with dynamic tables: http://stackoverflow.com/questions/10911707/jquery-quicksearch-in-dynamic-tables – frostyterrier Apr 25 '13 at 11:02
  • 1
    @frostyterrier Found the solution - updated above - thanks for the help :) – Darragh Verschoyle Apr 25 '13 at 12:55

2 Answers2

0

Check for errors in the console, as if something is going wrong it will suspend the script, preventing the later code from running.

Also, it looks like you are making a lot of HTTP requests there (one per line in the file) which is likely to be slow.

Appending stuff to the DOM before it is ready will also cause you problems. I would move all the code inside the $(document).ready(function() { });

Rik Heywood
  • 13,816
  • 9
  • 61
  • 81
  • Hi, thanks for your comment. I have tried this with the code both inside and outside of the document ready. Result is the same unfortunately. Also not seeing any errors in the console at the moment. – Darragh Verschoyle Apr 25 '13 at 09:19
0

I know whats the problem. you are making the ajax calls inside for loop and you are expecting for loop to wait till ajax is completed. right??

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


};

ajax call is asynchronous. so $.getJSON( will make call to server and will not wait in the for loop till it complete. as soon as it makes the ajax call, it will loop through the for loop. This will give you effect as if for loop not blocking the code next to it. ... but actually for loop is completing faster withh just rasing ajax requests and then next code get executed.

rahul maindargi
  • 5,359
  • 2
  • 16
  • 23
  • I do think that was part of the problem. I have figured out a way of getting it all to work now though. The quicksearch plugin allows for entries to be added to it's cache directly via javascript. I am using it now and everything works. :) – Darragh Verschoyle Apr 25 '13 at 11:29