2012-09-20 91 views
0

我有一個codeigniter應用程序,在我的一個視圖中,我有一個ajax調用返回json數據的API。一旦我得到數據,我通過它循環並將記錄追加到現有的表。 有兩種方法可以運行此ajax調用。一種是請求「全部」數據,另一種是按位置過濾。 這兩種方式都會返回數據,但是當Im試圖循環過濾數據集時,ajax失敗。迭代和顯示json數據

下面是通過記錄集循環代碼:

if (JSONdata.length != 0) 
{ 
     //create a heading row and attach it to the existing table. 
     var heading = $('<tr id="tblheading" naming="tblheading">').appendTo($('#switchrecords')); 
     heading.append($('<td>').append().text('Name')); 
     heading.append($('<td>').append().text('Object ID')); 

     //loop through each JSONdata item in the array and append another row to the switchrecords table. 
     $.each(JSONdata, function(i, objswitch) {       
      console.log('objectid is:'.objswitch.id); 
      console.log(objswitch.id); 
      var row = $('<tr>').appendTo($('#switchrecords')); 
      row.append($('<td>').append($('<a href='+ BASEPATH + 'index.php/controller/methodname/' + objswitch.id + '>').text(objswitch.name))); 
      row.append($('<td>').append(objswitch.id).text(objswitch.id)); 
}); 

這是我到目前爲止已經完成:

  1. 我確信,這兩個結果集具有相同的字段,即「id」和「name」。請注意,過濾的數據集包含比未過濾的結果更多的字段,但我認爲這不重要。

  2. 我已經使用console.log轉儲兩個結果集...這裏是兩個片段。第一個是ALL,另一個是過濾。

    [{「name」:「888-12-993-99-1」,「id」:「1」,「dict_value」:「compact」},{「name」:「888-22-SR1 -RTR-1" , 「ID」: 「2」, 「dict_value」: 「緊湊」},{ 「名稱」: 「888-21-SR1-SW-1」, 「ID」: 「3」,「dict_value 「:」compact「},{」name「:」888-11-SR2-SW-2「,」id「:」4「,」dict_value「:」compact「},....等等

    [{ 「PARENT_ID」: 「2」, 「TAG_ID」: 「10」, 「位置」: 「號樓」, 「ID」: 「7」, 「名稱」: 「888-22-228-22-1」, 「標籤」:NULL, 「asset_no」: 「1026067」, 「objtype_id」: 「1503」},{ 「PARENT_ID」: 「2」, 「TAG_ID」: 「5」, 「位置」: 「Building2」,「ID 「:」6「,」名稱「:」888-2-263-88-1「,」標籤「:空,」asset_no「:」1026068「,」objtype_id「:」1503「},....等

  3. 從代碼片段中可以看到,我試圖添加一些調試信息看看循環中發生了什麼。但是,我發現了以下錯誤消息在所述第一的console.log()調用:

    [17:13:30.675]類型錯誤: 「的objectid是:」 objswitch未定義

予。我不太清楚如何解決這個問題。任何建議,將不勝感激。 如果這是一個愚蠢的錯誤,我很抱歉!我一整天都在編程,我的腦子很好! =)

在此先感謝您的幫助。

回答

0

看起來你的語法錯誤拼接..它應該使用+,我看你使用的是 而不是objswitch嘗試使用此

$.each(JSONdata, function(i) {       
    console.log('objectid is:' + this.id); 
    console.log(this.id); 
    var row = $('<tr>').appendTo($('#switchrecords')); 
    row.append($('<td>').append($('<a href='+ BASEPATH + 'index.php/switches/getswitchdetails/' + this.id + '>').text(this.name))); 
    row.append($('<td>').append(this.id).text(this.id)); 
}); 

使用變量一般

$.each(JSONdata, function(i) {       
    console.log('objectid is:'+ JSONdata[i].id); 
    console.log(JSONdata[i].id); 
    var row = $('<tr>').appendTo($('#switchrecords')); 
    row.append($('<td>').append($('<a href='+ BASEPATH + 'index.php/switches/getswitchdetails/' + JSONdata[i].id + '>').text(JSONdata[i].name))); 
    row.append($('<td>').append(JSONdata[i].id).text(JSONdata[i].id)); 
}); 
+1

不知道怎麼的console.log'( 'OBJECTID是:' this.id);'沒有錯誤將執行。 '''應該導致它在你的具體情況下拋出錯誤:'未捕獲的SyntaxError:意外的字符串' – Nope

+0

@FrançoisWahl.. oops錯過了..謝謝指出它..我忽略了那裏的語法 –

+0

@sushanth!謝啦!就是這樣。我可以繼續前進並繼續排查問題! – dot

3

除非我完全脫落,它看起來像.objswitch應該+ objswitch否則試試。

嘗試的

console.log('objectid is:' + objswitch.id); 

代替

console.log('objectid is:'.objswitch.id); 
+0

!感謝您花時間回答。這是解決方案,但我給了sushanth對號,因爲他的console.log(JSON數據[i] .id);建議工作。但我會upvote你的答案! – dot

+0

@dot:如果您正在更新代碼以使用'JSONdata [i]',那麼您根本不需要使用'.each'。爲了更快的執行,你可以使用本地'for(i = 0; i Nope