javascript - Ajax in loop for html output - Bootstrap Tabs -


I want to retrieve more than one Jason files and keep the data of each file in the bootstrap 3 tab. I realize that getJSON is not called before the data is attached to the end of the function, but I do not understand the callback well. How do I set up a callback when I am adding HTML to a variable in an example below?

  function load tab (p) {var htmlTabs = '& lt; Ul class = "nav nav-tab" role = "tablestist" & gt; '; Var htmlContent = '& lt; Div class = "tab-content" & gt; '; For (var i = 1; i & lt; = p; i ++) {if (i == 0) {htmlTabs + = '& lt; Li class = "active" & gt; & Lt; A href = "'+ i +'" role = "tab" data-toggle = "tab" & gt; '+ I +' & lt; / A & gt; & Lt; / Li & gt; '; HtmlContent + = '& lt; Div class = "tab-panel active" id = "'+ i +" "">; } Other {htmlTabs + = '& lt; Li & gt; & Lt; A href = "# '+ i +' role =" tab "data-toggle =" tab "> gt; + i + ' & lt; / li & gt;'; HtmlContent + = '& Lt; div class = "tab-fane" id = "' + +" "> gt; ';} (function (i) {$ .getJSON (' sampleFile_0 '+ i +' .json ', function (Data) {}) .Done (function (data) {htmlContentnt + = '
& lt; / tr & gt;';} and if (ottid == data [ja] .tid) {htmlContent + = '& lt; tr & gt; & Lt; td> '+ data [j] .cl +' ';}} htmlContent + =' & lt; / table & gt; ' ;})}) (I); htm LContent + = '& lt; / div & gt;';}; HtmlTabs + = '& lt; / ul & gt;'; HtmlContent + = '& lt; / div & gt;' $ ('# block' ) Add (html tabs + html contact);}

You already make promises , So the trick here is that you need to capture all those promises and use outside of your loop only when all the promises are fulfilled you should start it:

  var promises = []; For (var i = 1; i & lt; = p; i ++) {if (i == 0) {htmlTabs + = '& lt; Li class = "active" & gt; & Lt; A href = "'+ i +'" role = "tab" data-toggle = "tab" & gt; '+ I +' & lt; / A & gt; & Lt; / Li & gt; '; HtmlContent + = '& lt; Div class = "tab-panel active" id = "'+ i +" "">; } Other {htmlTabs + = '& lt; Li & gt; & Lt; A href = "# '+ i +' role =" tab "data-toggle =" tab "> gt; + i + ' & lt; / li & gt;'; HtmlContent + = '& Lt; div class = "tab-fane" id = "' + +" "> gt; ';} (function (i) {facebook pash ($ .getJSON (' sampleFile_0 '+ i +' .json '); (For example, june = 0; june;); done (function (data) {htmlContentnt + =' & lt; div class = "table-responsive" & gt; Table class = "table" & gt; Lt; data; long; J ++) {if (ttid == data [ja] .tid) {htmlContent + = '<& lt; t & gt;' td & gt; '+ data [j] .cL & lt; / td> & lt; / tr & gt; & # 39;;;; and if (ottid == data [ja] tid) {htmlContent + = '<& lt; t & lt; ; Td> '+ data [j] .cl' '/ td> & lt; / tr & gt;';}} h TmlContent + = '& lt; / table & gt;';}));}) (i); HtmlContent + = '& lt; / div & gt;';}; ... $. , Donations (function () {$ ('# block'). Attachments (html tabs + htmlcontent);});    

Comments

Popular posts from this blog

java - ImportError: No module named py4j.java_gateway -

python - Receiving "KeyError" after decoding json result from url -

.net - Creating a new Queue Manager and Queue in Websphere MQ (using C#) -