簡體   English   中英

陣列數據不會顯示在表格中

[英]Array data wont display in a table

我有一個ajax函數,該函數將get請求發送到api並以以下格式返回數據-

{
    "firstname": "John",
    "lastname": "Doe",
    "email": "doej@gmail.com",
    "subjects": [
      {
        "id": 1,
        "name": "maths"
      },
      {
        "id": 2,
        "name": "chemistry"
      }
    ]
  },

我需要在一個表中顯示此數據,但在使subjects數組正確顯示(即作為一個表單元格中的列表)時遇到麻煩。 我試圖將數組數據放入主表中的另一個表中,但是無法正常工作。 我想我在迭代循環中某個地方犯錯了。

function getPeople() {


     $.ajax({
         type: "GET",
         url: "http://example.com",
         contentType: "application/json; charset=utf-8",
         crossDomain: true,
         dataType: "json",
         success: function (data, status, jqXHR) {



             // fill a table with the JSON
           $("table.mytable").html("<tr><th></th><th>First Name</th><th>Last Name</th><th>Subjects</th></tr>"  );

for (var i = 0; i < data.length; i++) {

        for (var j = 0; j < data[i].subjects.length; j++) {

          var subjects = data[i].subjects[j];
        $("table.insidetable").append('<tr><td>' + subjects + 'tr><td>')
      }

    $("table.mytable").append('<tr><td><input type = "checkbox" id = '+data[i].id+'>'  + "</td><td>" + data[i].firstname + "</td><td>" + data[i].lastname + "</td><td>" + "table.insidetable"  + "</td></tr>");
}

         },

         error: function (jqXHR, status) {
             // error handler
             console.log(jqXHR);
             alert('fail' + status.code);
         }
      });
   }

下面是工作(經過測試)的代碼。

var data = [{
  "firstname": "John",
  "lastname": "Doe",
  "email": "doej@gmail.com",
  "subjects": [
    {
        "id": 1,
    "name": "maths"
    },
    {
        "id": 2,
        "name": "chemistry"
    }
        ]
},
{
  "firstname": "Steve",
  "lastname": "Gentile",
  "email": "steve@gmail.com",
  "subjects": [
    {
    "id": 1,
    "name": "history"
    },
    {
        "id": 2,
    "name": "geography"
    }
  ]
}];

$("table.mytable").html("<tr><th></th><th>First Name</th><th>Last Name</th><th>Subjects</th></tr>");

for (var i = 0; i < data.length; i++) {
    var subjectList = '';

    for (var j = 0; j < data[i].subjects.length; j++) {
      subjectList += '<li>' + data[i].subjects[j].name + '</li>';
    }

    $("table.mytable").append('<tr><td><input type="checkbox" id=' + i +'/></td><td>' + data[i].firstname + '</td><td>' + data[i].lastname + '</td><td><ul>' + subjectList  + '</ul></td></tr>');
}

在下面的語句中似乎有很多問題,例如,

  • 標簽未正確關閉
  • 正在使用不存在的屬性(data [i] .id)
  • 單引號和雙引號不匹配

...

$("table.mytable").append('<tr><td><input type = "checkbox" id = '+data[i].id+'>'  + "</td><td>" +data[i].firstname + "</td><td>" + data[i].lastname + "</td><td>" + "table.insidetable"+ "</td></tr>");

猜想您是否錯過了'<'

在該行中:

 $("table.insidetable").append('<tr><td>' + subjects + 'tr><td>')

$("table.mytable").append('<tr><td><input type = "checkbox" id = '+data[i].id+'>'  + "</td><td>" + data[i].firstname + "</td><td>" + data[i].lastname + "</td><td>" + "table.insidetable"  + "</td></tr>");

我不認為您應該附加“ table.insidetable”嗎? 這將顯示為值“ table.insidetable”,而不是表的內容。 認為這應該像$("table.insidetable").val()

暫無
暫無

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

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