2017-12-22 12 views
5

入れ子になったJSONデータをHTMLテーブルに変換しようとしていますが、エラーが発生しています。入れ子になったJSONデータをテーブルに変換する

どこが間違っているのか分かりません。おそらくオブジェクト内の配列にアクセスする方法に何か問題がありますか? 、Chromeで

function DonutTable(array){ 
    //create a table element 
    var table = document.createElement("table"); 
    //create header columns 

    var col = Object.keys(array[0]); //array of keys 
    //write keys onto the header cell 
    var tr = table.insertRow(-1); 
    col.forEach(function(key){ 
     var th = document.createElement("th"); 
     th.textContent = key; 
     tr.appendChild(th); 
    }); 

    //create rows to hold the rest of the data 
    array.forEach(function(obj){ 
     //for each obj in the main array, create a row 
     var data_row = table.insertRow(-1); 
     //for each header in the col array, populate data 
     col.forEach(function(key){ 
      var tabCell = data_row.insertCell(-1); 
      if (key==="batters"){ 
       //grab the value of batters and access value of batter 
       obj["batters"]["batter"].forEach(function(e){ 
        //for each e in batter, create a div element 
        var div = document.createElement("div"); 
        //write on the div 
        div.textContent = e.type + "(" + e.id + ")"; 
        tabCell.appendChild(div); }) 
       } 
      if (Array.isArray(obj[key])){ //check if a value of a key is an array 
       obj[key].forEach(function(topping){ 
        //for each obj in topping, get id and type 
        var div = document.createElement("div"); 
        div.textContent = topping.type + "(" + topping.id + ")"; 
        tabCell.appendChild(div); 
       }) 
      } 
      else{ 
       tabCell.textContent = obj[key]; 
      } 


       }) 
      }) 


    var divContainer = document.getElementById("showTable"); 
    divContainer.innerHTML = ""; 
    divContainer.appendChild(table); 

} 

var donut = [ 
    { 
     "id": "0001", 
     "type": "donut", 
     "name": "Cake", 
     "ppu": 0.55, 
     "batters": 
      { 
       "batter": 
        [ 
         { "id": "1001", "type": "Regular" }, 
         { "id": "1002", "type": "Chocolate" }, 
         { "id": "1003", "type": "Blueberry" }, 
         { "id": "1004", "type": "Devil's Food" } 
        ] 
      }, 
     "topping": 
      [ 
       { "id": "5001", "type": "None" }, 
       { "id": "5002", "type": "Glazed" }, 
       { "id": "5005", "type": "Sugar" }, 
       { "id": "5007", "type": "Powdered Sugar" }, 
       { "id": "5006", "type": "Chocolate with Sprinkles" }, 
       { "id": "5003", "type": "Chocolate" }, 
       { "id": "5004", "type": "Maple" } 
      ] 
    }, 
    { 
     "id": "0002", 
     "type": "donut", 
     "name": "Raised", 
     "ppu": 0.55, 
     "batters": 
      { 
       "batter": 
        [ 
         { "id": "1001", "type": "Regular" } 
        ] 
      }, 
     "topping": 
      [ 
       { "id": "5001", "type": "None" }, 
       { "id": "5002", "type": "Glazed" }, 
       { "id": "5005", "type": "Sugar" }, 
       { "id": "5003", "type": "Chocolate" }, 
       { "id": "5004", "type": "Maple" } 
      ] 
    }, 
    { 
     "id": "0003", 
     "type": "donut", 
     "name": "Old Fashioned", 
     "ppu": 0.55, 
     "batters": 
      { 
       "batter": 
        [ 
         { "id": "1001", "type": "Regular" }, 
         { "id": "1002", "type": "Chocolate" } 
        ] 
      }, 
     "topping": 
      [ 
       { "id": "5001", "type": "None" }, 
       { "id": "5002", "type": "Glazed" }, 
       { "id": "5003", "type": "Chocolate" }, 
       { "id": "5004", "type": "Maple" } 
      ] 
    } 
] 
DonutTable(donut); 

<html> 
    <head> 
     <title>HTML Donut Table from JSON</title> 
     <script type="text/javascript" src="script.js"></script> 
    </head> 
    <body> 
     <input type="button" value="Generate a table" onclick="DonutTable()"> 
     <div id="showTable"></div> 
    </body> 
</html> 
+0

これはあなたの実際のコードである場合は、ページが完全にレンダリングされる前に、HTML要素にアクセスしようとすることがあります。 –

答えて

2

あなたdivContainerを宣言した直後にブレークポイントを設定します。私が書いたコードを以下に

"Cannot set property 'innerHTML' of null"

さ:

それは、このエラーを投げ続けました。コードに基づいて、divContainerはnullです。これは、ページ上のHTMLの前にJavaScriptを実行しているためです。 JSをdocument.readyタイプの関数に移動するか、スクリプトセクションをHTMLの下に移動します。

+0

ありがとうございます。私のテーブルは今表示されます。しかし打者セクションは正しく表示されませんでした。 [オブジェクト、オブジェクト]が表示されます。つまり、オブジェクト内の配列にアクセスするために使用するforEachループに問題があります。 – ChuChu

2

これは私がJSとHTMLの部分に分割した正確なコードです。

donutの配列がDonutTable()関数に明示的に渡されているため、最初の実行時に機能します。あなたのHTMLがパラメータなしでDonutTable()を呼び出すので、ボタンクリックでは機能しません。

function DonutTable(array){ 
 
    //create a table element 
 
    var table = document.createElement("table"); 
 
    //create header columns 
 

 
    var col = Object.keys(array[0]); //array of keys 
 
    //write keys onto the header cell 
 
    var tr = table.insertRow(-1); 
 
    col.forEach(function(key){ 
 
     var th = document.createElement("th"); 
 
     th.textContent = key; 
 
     tr.appendChild(th); 
 
    }); 
 

 
    //create rows to hold the rest of the data 
 
    array.forEach(function(obj){ 
 
     //for each obj in the main array, create a row 
 
     var data_row = table.insertRow(-1); 
 
     //for each header in the col array, populate data 
 
     col.forEach(function(key){ 
 
      var tabCell = data_row.insertCell(-1); 
 
      if (key==="batters"){ 
 
       //grab the value of batters and access value of batter 
 
       obj["batters"]["batter"].forEach(function(e){ 
 
        //for each e in batter, create a div element 
 
        var div = document.createElement("div"); 
 
        //write on the div 
 
        div.textContent = e["type"] + "(" + e["id"] + ")"; 
 
        tabCell.appendChild(div); }) 
 
       } 
 
      else if (Array.isArray(obj[key])){ //check if a value of a key is an array 
 
       obj[key].forEach(function(topping){ 
 
        //for each obj in topping, get id and type 
 
        var div = document.createElement("div"); 
 
        div.textContent = topping.type + "(" + topping.id + ")"; 
 
        tabCell.appendChild(div); 
 
       }) 
 
      } 
 
      else{ 
 
       tabCell.textContent = obj[key]; 
 
      } 
 

 

 
       }) 
 
      }) 
 

 

 
    var divContainer = document.getElementById("showTable"); 
 
    divContainer.innerHTML = ""; 
 
    divContainer.appendChild(table); 
 

 
} 
 

 
var donut = [ 
 
    { 
 
     "id": "0001", 
 
     "type": "donut", 
 
     "name": "Cake", 
 
     "ppu": 0.55, 
 
     "batters": 
 
      { 
 
       "batter": 
 
        [ 
 
         { "id": "1001", "type": "Regular" }, 
 
         { "id": "1002", "type": "Chocolate" }, 
 
         { "id": "1003", "type": "Blueberry" }, 
 
         { "id": "1004", "type": "Devil's Food" } 
 
        ] 
 
      }, 
 
     "topping": 
 
      [ 
 
       { "id": "5001", "type": "None" }, 
 
       { "id": "5002", "type": "Glazed" }, 
 
       { "id": "5005", "type": "Sugar" }, 
 
       { "id": "5007", "type": "Powdered Sugar" }, 
 
       { "id": "5006", "type": "Chocolate with Sprinkles" }, 
 
       { "id": "5003", "type": "Chocolate" }, 
 
       { "id": "5004", "type": "Maple" } 
 
      ] 
 
    }, 
 
    { 
 
     "id": "0002", 
 
     "type": "donut", 
 
     "name": "Raised", 
 
     "ppu": 0.55, 
 
     "batters": 
 
      { 
 
       "batter": 
 
        [ 
 
         { "id": "1001", "type": "Regular" } 
 
        ] 
 
      }, 
 
     "topping": 
 
      [ 
 
       { "id": "5001", "type": "None" }, 
 
       { "id": "5002", "type": "Glazed" }, 
 
       { "id": "5005", "type": "Sugar" }, 
 
       { "id": "5003", "type": "Chocolate" }, 
 
       { "id": "5004", "type": "Maple" } 
 
      ] 
 
    }, 
 
    { 
 
     "id": "0003", 
 
     "type": "donut", 
 
     "name": "Old Fashioned", 
 
     "ppu": 0.55, 
 
     "batters": 
 
      { 
 
       "batter": 
 
        [ 
 
         { "id": "1001", "type": "Regular" }, 
 
         { "id": "1002", "type": "Chocolate" } 
 
        ] 
 
      }, 
 
     "topping": 
 
      [ 
 
       { "id": "5001", "type": "None" }, 
 
       { "id": "5002", "type": "Glazed" }, 
 
       { "id": "5003", "type": "Chocolate" }, 
 
       { "id": "5004", "type": "Maple" } 
 
      ] 
 
    } 
 
] 
 
DonutTable(donut);
<html> 
 
    <head> 
 
     <title>HTML Donut Table from JSON</title> 
 
     <script type="text/javascript" src="script.js"></script> 
 
    </head> 
 
    <body> 
 
     <input type="button" value="Generate a table" onclick="DonutTable()"> 
 
     <div id="showTable"></div> 
 
    </body> 
 
</html>

+0

ありがとうございます。私の作品は今でも使えます。しかし、 "打者"の列に何か問題があります。私はオブジェクト内の配列にアクセスし、forEachは配列内の各objをループして書き出しましたが、それでも表示される:[object Object]。なぜなのかご存知ですか? – ChuChu

+0

コードのコメントをご覧ください。 –

+0

ありがとうございますが、else文を使用しないと、他のカラムは埋められません – ChuChu

関連する問題