将外部JSON文件读入动态HTML表

时间:2017-11-05 14:15:38

标签: jquery html css json

我正在尝试使用jQuery将JSON文件读入动态HTML表。具体来说,我正在尝试在body类中的div内创建表。我一直在关注http://www.encodedna.com/jquery/read-json-file-push-data-into-array-and-convert-to-html-table-using-jquery.htm但是当我查看网页时,表格没有被创建。我确定我已将$ .getJSON指向正确的文件路径。如果您想查看我将用它编辑的任何特定代码。提前谢谢。

修改 - 添加脚本代码

<script type="text/javascript">
    $(document).ready(function() {
        $.getJSON("/js/na_lcs_results.json", function(data) {

            var arrItems = [];
            $.each(data, function(index, value) {
                arrItems.push(value);
            });

            //Extract value for table header
            var col = [];
            for (var i = 0; i < arrItems.length; i++) {
                if (col.indexOf(key) === -1) {
                    col.push(key);
                }
            }
        }

        //Create dynamic table
        var table = document.createElement("table");

        //Create HTML table header using extracted headers
        var tr = table.insertRow(-1);

        for (var i = 0; i < col.length; i++) {
            var th = document.createElement("th");
            th.innerHTML = col[i];
            tr.appendChild(th);
        }

        //Add JSON data to table as rows
        for (var i = 0; i < arrItems.length; i++) {
            tr = table.insertRow(-1);
            for (var j = 0; j < col.length; j++) {
                var tabCell = tr.insertCell(-1);
                tabCell.innerHTML = arrItems[i][col[j]];
            }
        }

        //Add new table with JSON data to container
        var divContainer = document.getElementById("showData");
        divContainer.innerHTML = "";
        divContainer.appendChild(table);
        });
    });

修改 - 我想要添加到

的Div类
<div class="NA_LCS">
<h1>Welcome to the NA LCS page</h1>
    <div id="showData"></div>
</div>

修改 - 表格的外部CSS页面

table, th, td {
margin: 10px 0;
border: solid 1px #333;
padding: 2px 4px;
font: 15px Abel; }

< script >
  $('#home_button').click(function() {
    $('.home_screen').fadeIn();
    $('#mySidebar,.EU_LCS,.NA_LCS').fadeOut();
  });

$('#EU_LCS_button').click(function() {
  $('.EU_LCS').fadeIn();
  $('#mySidebar,.home_screen,.NA_LCS').fadeOut();
});

$('#NA_LCS_button').click(function() {
  $('.NA_LCS').fadeIn();
  $('#mySidebar,.home_screen,.EU_LCS').fadeOut();
}); <
/script>

<
script >
  function w3_open() {
    document.getElementById("mySidebar").style.display = "block";
  }

function w3_close() {
  document.getElementById("mySidebar").style.display = "none";
} <
/script>


<!-- <script type="text/javascript">
$(document).ready(function() {
  $.getJSON("/js/na_lcs_results.json", function(data) {

      var arrItems = [];
      $.each(data, function(index, value) {
        arrItems.push(value);
      });

      //Extract value for table header
      var col = [];
      for (var i = 0; i < arrItems.length; i++) {
        if (col.indexOf(key) === -1) {
          col.push(key);
        }
      }
    }

    //Create dynamic table
    var table = document.createElement("table");

    //Create HTML table header using extracted headers
    var tr = table.insertRow(-1);

    for (var i = 0; i < col.length; i++) {
      var th = document.createElement("th");
      th.innerHTML = col[i];
      tr.appendChild(th);
    }

    //Add JSON data to table as rows
    for (var i = 0; i < arrItems.length; i++) {
      tr = table.insertRow(-1);
      for (var j = 0; j < col.length; j++) {
        var tabCell = tr.insertCell(-1);
        tabCell.innerHTML = arrItems[i][col[j]];
      }
    }

    //Add new table with JSON data to container
    var divContainer = document.getElementById("showData"); divContainer.innerHTML = ""; divContainer.appendChild(table);
  });
}); -->
<
/script>

<
script >
  $(document).ready(function() {
    $('#tabs').tabs();
    $("accordion1").accordion();
    $("#accordion2").accordion();
  }); <
/script>
#mySidebar img {
  width: 32px;
  height: 32px;
}

.home_screen {
  margin: 70px;
}

.NA_LCS {
  display: none;
  margin: 70px;
}

.EU_LCS {
  display: none;
  margin: 70px;
}

body {
  background-color: #8c8c8c;
}

#banner {
  background-image: url('../img/lol_universe_bg.jpg');
  background-size: cover;
  background-position: center;
  width: 800;
  height: 300px;
}

h1 {
  font-family: 'Abel';
  font-size: 48px;
  text-align: center;
}

table,
th,
td {
  margin: 10px 0;
  border: solid 1px #333;
  padding: 2px 4px;
  font: 15px Abel;
}

th {
  font-weight: bold:
}
<!DOCTYPE html>
<html>

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">

  <title>LoL Universe</title>

  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

  <link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
  <link rel="stylesheet" type="text/css" href="https://fonts.googleapis.com/css?family=Abel">
  <link rel="stylesheet" type="text/css" href="css/mystylesheet.css">
</head>

<body>

  <div class="w3-sidebar w3-bar-block w3-black w3-animate-left" style="display:none" id="mySidebar">
    <button class="w3-bar-item w3-button w3-large" onclick="w3_close()">Close &times;</button>
    <a href="#" class="w3-bar-item w3-button" id="home_button"><img src="img/icon_home.png"></a>
    <a href="#" class="w3-bar-item w3-button" id="EU_LCS_button"><img src="img/EU_LCS_logo.png"></a>
    <a href="#" class="w3-bar-item w3-button" id="NA_LCS_button"><img src="img/NA_LCS_logo.png"></a>
  </div>

  <div class="nav_button">
    <button class="w3-button w3-white w3-xxlarge" onclick="w3_open()">&#9776;</button>
  </div>

  <div id="banner"></div>

  <div class="home_screen">
    <h1>Welcome to LoL Universe</h1>
  </div>

  <div class="EU_LCS">
    <h1>Welcome to the EU LCS page</h1>
  </div>

  <div class="NA_LCS">
    <h1>Welcome to the NA LCS page</h1>
    <!-- <div id="showData"></div> -->
  </div>

</body>

</html>

1 个答案:

答案 0 :(得分:0)

尽管我希望您已经找到了解决方案,但这里有一些更多详细信息可以帮助人们登陆此页面。

似乎有些小的修复可以使您的代码起作用(在列表中,使用jQuery从本地JSON文件数据中创建动态TABLE)。 UI / CSS方面不会在此答案中得到解决。

简而言之,当使用 encodedna.com 片段时,请正确复制它们:)

要在此处解决您的问题:首先(尽管不是必需的)创建一个单独的JavaScript文件,以使HTML简短易读...

myjavascript.js

$(document).ready(function() {

    $.getJSON("na_lcs_results.json", function(data) {
        var arrItems = [];
        $.each(data, function(index, value) {
            arrItems.push(value);
        });

        // extract value for table header
        var col = [];
        for (var i = 0; i < arrItems.length; i++) {
            for (var key in arrItems[i]) {
                if (col.indexOf(key) === -1) {
                    col.push(key);
                }
            }
        }
        // create dynamic table
        var table = document.createElement("table");

        // create HTML table header using extracted headers
        var tr = table.insertRow(-1);

        for (var i = 0; i < col.length; i++) {
            var th = document.createElement("th");
            th.innerHTML = col[i];
            tr.appendChild(th);
        }

        // add JSON data to table as rows
        for (var i = 0; i < arrItems.length; i++) {
            tr = table.insertRow(-1);
            for (var j = 0; j < col.length; j++) {
                var tabCell = tr.insertCell(-1);
                tabCell.innerHTML = arrItems[i][col[j]];
            }
        }

        // add new table with JSON data to container
        var divContainer = document.getElementById("showData");
        divContainer.innerHTML = "";
        divContainer.appendChild(table);
    });
});

$('#home_button').click(function() {
    $('.home_screen').fadeIn();
    $('#mySidebar,.EU_LCS,.NA_LCS').fadeOut();
});

$('#EU_LCS_button').click(function() {
    $('.EU_LCS').fadeIn();
    $('#mySidebar,.home_screen,.NA_LCS').fadeOut();
});

$('#NA_LCS_button').click(function() {
    $('.NA_LCS').fadeIn();
    $('#mySidebar,.home_screen,.EU_LCS').fadeOut();
});

function w3_open() {
    document.getElementById("mySidebar").style.display = "block";
}

function w3_close() {
    document.getElementById("mySidebar").style.display = "none";
}

/*
$(document).ready(function() {
    $('#tabs').tabs();
    $("accordion1").accordion();
    $("#accordion2").accordion();
});
*/

然后从以前的代码中删除所有JavaScript / jQuery代码,并按如下所示更新 HTML代码

...
  <div class="NA_LCS">
    <h1>Welcome to the NA LCS page</h1>
    <div id="showData"></div>
  </div>

  <script type="text/javascript" src="myjavascript.js"></script>

</BODY>
...

也许更好地构造代码并以干净的代码序列询问问题可能会有所帮助...希望这会有所帮助!

相关问题