2012-02-11 56 views
0

我有一個最令人沮喪的問題,我曾與編程語言。我讀了一些XML,然後嘗試顯示在網頁上。這樣做我沒有問題。 這裏是如何完成這個代碼。更新與循環對象與網頁的網頁

    // File: readXML.js 

         var shared  = []; 
         var sheet  = new Array() 
         // Start function when DOM has completely loaded 
         $(document).ready(function(){ 
          var bigo  = new Object(); 
          console.log("can you see me."); 

          var sheetJoint = new Object(); 


          // get the sheet xml file 
          $.get("sheet1.xml",{},function(xml){ 
           var attrs = []; 

           // this is a loop within a loop. we traverse the values in the xml to get end up with a key pair value of key: val 
          // in our case this works out to be A1 = 0 this is the first step to get the actual value from the sharedstring.xml 
          // Run the function for each row tag in the XML file 
          $(xml).find("row").each(function(i) { 
           //run the function for each c tag in the xml and get the attribute. 
           //this is the attribute that references the actual column. 
           $(this).find("c").each(function(i){ 
            $('c',xml).each(function(i) { 
              v1 = $(this).attr("r"); 
             bigo[v1] =v1; 
              bigo[v1]= $(this).find("v").text(); 
           }); 
         })}); 

          //get the shared string elements to combine with the other 
          $.get("sharedStrings.xml",{},function(xml){ 
            $('si',xml).each(function(i) { 
             shared.push($(this).find("t").text()); 

           })}); 


          }); 
          combineObjects(bigo);//combine the the array and the object. 
         }); 

因爲我有兩個讀取兩個不同的XML文件,我不得不使用另一個函數來組合它們。這是這個功能。

function combineObjects(obj){ 
          myHTMLOutput = ''; 
          myHTMLOutput += '<table width="98%" border="1" cellpadding="0" cellspacing="0">'; 
          myHTMLOutput += '<th>A</th>'; 


         //mydiv=document.getElementById("ContentArea") 

        try{ 
         var strt=""; 
         var tempVal; 
        //loop throught the obejct and get the value from the returnTheValueSegment. 

        for (var ind in obj){ 
         //if you want to print something to the log then just add this. 
         // pretty handy when trying to discover variable values. does not see to work well inside for loops thought. 
         // console.log("can you see me."); 
         tempVal = returnTheValueOfSegment(obj[ind]); 
        //bring the values 
         obj[ind] = tempVal; 

         } 

         for (var ind in obj){ 


           mydata = BuildStudentHTML(ind); 
           myHTMLOutput = myHTMLOutput + mydata; 


         } 
          myHTMLOutput += '</table>'; 
           $("#ContentArea").append(myHTMLOutput); 
        } 
         catch(err){alert(err)}; 

        } 

我在創建表時出現問題。它的基本命中或錯過... 如果我在Firefox中嘗試它,它只能工作,如果我使用螢火蟲,並通過代碼,否則它不會顯示錶格元素。

這裏是被調用來創建表的代碼。

     function BuildStudentHTML(column1){ 



          // Build HTML string and return 
          output = ''; 
          output += '<tr>'; 
          output += '<td>'+ column1 +'</td>'; 
          output += '</tr>'; 
          return output; 
         } 

我可能做錯了什麼。我需要某種計時器嗎?是循環是快速和頁面不能刷新。如果有人能指出我正確的方向,我會永遠感激。

+0

你知道'$ .get'是**異步**,對吧?並且在HTTP請求完成之前,返回的對象將不可用?您需要將返回的Deferred對象與'$ .when()'結合起來,然後在回調函數中完成「組合」工作。 – Pointy 2012-02-11 13:41:34

+0

感謝您的答覆pointy.but你是否基本上說,即使在$ .get完成之前,該功能正在前進?您能否通過.get回撥指向正確的方向?我真的很新,並試圖瞭解jQuery。 – user677275 2012-02-11 13:52:04

+0

是 - '$ .get()'立即返回**,即使它可能在HTTP請求完成之前有許多毫秒或幾秒。看@ wecsam的答案。 – Pointy 2012-02-11 14:03:25

回答

1

在您的代碼中,在XML文件的HTTP請求可以完成之前調用combineObjects(bigo);。當請求完成加載時,$.get()啓動一個新的HTTP請求,然後運行成功功能您可以嘗試將combineObjects(bigo);放入最後一個XML文檔的成功函數中,但這不起作用,因爲bigo將在該函數中未定義。解決方案是創建一個函數來創建一個函數。在$(document).ready()函數之前將這個:

function second_XML(bigo){ 
    return function(xml){ 
     $('si', xml).each(function (i) { 
      shared.push($(this).find("t").text()); 
     }); 
     combineObjects(bigo); //combine the the array and the object. 
    } 
} 

這可以讓你的bigo變量傳遞給函數作爲外部變量。接下來,更換與此加載第二個XML文檔中的代碼:

//get the shared string elements to combine with the other 
$.get("sharedStrings.xml", {}, second_XML(bigo)); 

這將使代碼等到第二XML文件兩者結合之前加載。出於某種原因,在加載第二個XML文檔之前,您已經讓您的代碼等待加載第一個XML文檔,因此您在那裏沒有問題。

+1

謝謝你我完全錯過了異步部分。你是誰,謝謝你指引我朝着正確的方向。 – user677275 2012-02-11 17:40:16

+0

沒問題。不用謝。 – wecsam 2012-02-12 18:49:06