簡體   English   中英

遍歷並顯示json數據

[英]Iterating through and displaying json data

我有一個codeigniter應用程序,在我的一種觀點中,我有一個對返回json數據的API的ajax調用。 一旦獲得數據,便會遍歷數據並將記錄追加到現有表中。 有兩種方法可以運行此ajax調用。 一種是請求“所有”數據,另一種是按位置過濾。 兩種方法都返回數據,但是當Im試圖遍歷過濾后的數據集時,ajax失敗了。

這是遍歷記錄集的代碼:

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,另一個被過濾。

    [{“名稱”:“ 888-12-993-99-1”,“ id”:“ 1”,“ dict_value”:“緊湊”},{“名稱”:“ 888-22-SR1-RTR-1 “,” id“:” 2“,” dict_value“:” compact“},{” name“:” 888-21-SR1-SW-1“,” id“:” 3“,” dict_value“:” compact “},{” name“:” 888-11-SR2-SW-2“,” id“:” 4“,” dict_value“:” compact“},.. etc

    [{{“ parent_id”:“ 2”,“ tag_id”:“ 10”,“ Location”:“ Building1”,“ id”:“ 7”,“ name”:“ 888-22-228-22-1”, “ label”:null,“ asset_no”:“ 1026067”,“ objtype_id”:“ 1503”},{“ parent_id”:“ 2”,“ tag_id”:“ 5”,“ Location”:“ Building2”,“ id “:” 6“,”名稱“:” 888-2-263-88-1“,”標簽“:空,” asset_no“:” 1026068“,” objtype_id“:” 1503“},....等。

  3. 從代碼片段中可以看到,我嘗試添加一些調試信息以查看循環中發生了什么。 但是,我在第一個console.log()調用中收到以下錯誤消息:

    [17:13:30.675] TypeError:“對象ID為:”。objswitch未定義

我不太確定該如何解決。 任何建議,將不勝感激。 如果這是一個愚蠢的錯誤,我提前致歉! 我整天都在編程,我的大腦糊塗了! =)

先謝謝您的幫助。

除非我完全不喜歡,否則看起來.objswitch應該是+ objswitch

嘗試

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

代替

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

看起來您的語法連接錯誤。.應該使用+完成,我看到您使用的是 代替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));
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM