简体   繁体   English

遍历并显示json数据

[英]Iterating through and displaying json data

I have a codeigniter app and in one of my views, i have an ajax call to an API that returns json data. 我有一个codeigniter应用程序,在我的一种观点中,我有一个对返回json数据的API的ajax调用。 Once i get the data, I loop through it and append records to an existing table. 一旦获得数据,便会遍历数据并将记录追加到现有表中。 There are two ways to run this ajax call. 有两种方法可以运行此ajax调用。 One is to request "all" data, the other is to filter by location. 一种是请求“所有”数据,另一种是按位置过滤。 Both ways return data, but when Im trying to loop through the filtered data set, the ajax is failing. 两种方法都返回数据,但是当Im试图遍历过滤后的数据集时,ajax失败了。

Here's the code that loops through the record set: 这是遍历记录集的代码:

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

Here's what I've done so far: 到目前为止,这是我所做的:

  1. I've made sure that both result sets have the same fields, namely "id" and "name". 我确保两个结果集都具有相同的字段,即“ id”和“ name”。 Mind you, the filtered data set includes more fields than the non filtered result but i don't think that should matter. 请注意,经过过滤的数据集比未经过滤的结果包含更多的字段,但我认为这并不重要。

  2. I've used console.log to dump both result sets... Here's a snippet from both. 我已经使用console.log来转储这两个结果集...这是这两个的摘要。 The first one is ALL and the other is filtered. 第一个是ALL,另一个被过滤。

    [{"name":"888-12-993-99-1","id":"1","dict_value":"compact"},{"name":"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 [{“名称”:“ 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","name":"888-2-263-88-1","label":null,"asset_no":"1026068","objtype_id":"1503"}, .... 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. As you can see from the code snippet, I've tried to add some debug information to see what's happening inside the loop. 从代码片段中可以看到,我尝试添加一些调试信息以查看循环中发生了什么。 However, I'm getting the following error message on the first console.log() call: 但是,我在第一个console.log()调用中收到以下错误消息:

    [17:13:30.675] TypeError: "objectid is:".objswitch is undefined [17:13:30.675] TypeError:“对象ID为:”。objswitch未定义

I'm not really too sure how to resolve this. 我不太确定该如何解决。 Any suggestions would be appreciated. 任何建议,将不胜感激。 I apologize in advance if it's a silly mistake! 如果这是一个愚蠢的错误,我提前致歉! I've been programming all day and my brain is mush! 我整天都在编程,我的大脑糊涂了! =) =)

Thanks in advance for the help. 先谢谢您的帮助。

Unless I'm completely off it looks like .objswitch should be + objswitch . 除非我完全不喜欢,否则看起来.objswitch应该是+ objswitch

Try 尝试

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

instead of 代替

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

Looks like your syntax is wrong for concatenation .. It should be done using a + , I see you are using a . 看起来您的语法连接错误。.应该使用+完成,我看到您使用的是 Instead of objswitch try using this 代替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));
});

Otherwise try using the variable in general 否则,一般尝试使用变量

$.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