html JS.JSON.FetchingData.PopulateTable.ex8

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了html JS.JSON.FetchingData.PopulateTable.ex8相关的知识,希望对你有一定的参考价值。

@import url('https://fonts.googleapis.com/css?family=Kirang+Haerang');
* {
  border: none;
  outline: none;
  margin: 0;
  padding: 0;
}

body {
  font-size: 1rem;
  color: #555;
}

legend {
  text-align: center;
  font-size: 2.5rem;
  font-family: 'Kirang Haerang', cursive;
}

button {
     background-color: #008CBA; 
    border-radius: 20px;
    color: white;
    padding: 15px 32px;
    text-align: center;
    text-decoration: none;
    display: block;
    font-size: 16px;
    margin:  2% auto;
    cursor: pointer;
  
}

.table-container {
  min-width: 50%;
  max-width: 70%;
  margin: auto;
}

table {
  border: 1px solid;
  border-color:#D8BFD8;
  border-collapse: collapse;
  width: 100%;
  margin: 5% auto;
}

caption {
  margin-bottom: 2%;
}

thead, tfoot {
  background-color: #008080;  
  color: white;
  font-weight: 300;
}

th, td {
  border: 1px;
  padding: 6px;
  text-align: center;
}

tbody tr:nth-child(even) {
  background-color: #F5DEB3;
} 
var searchList;


window.onload = function init(){
  console.log('DOM is ready');
  
  searchList = document.querySelector('.list');
  
  searchList.addEventListener('click', list);
}


function list(evt) {
 
  var url = 'https://gist.githubusercontent.com/heiswayi/7fde241975ed8a80535a/raw/ff1caaeaf62bd6740ab7cafcd61f1215de173379/datatables-data.json';

  
  fetch(url)
    
   
    .then(function(data){
      
      return data.json();
    })
    .then(function(data){
      
      buildTableFor(data);
    })
     
    .catch(function(err){
      console.log('The following error has been occured ' + err);
    });
}

function buildTableFor(users) {
  
  
  var tableContainer = document.querySelector('.table-container');
   
  tableContainer.innerHTML = '';
  
  
  var table=  document.createElement('table');
  
  var caption = table.createCaption();
  
  var thead = table.createTHead();
  
  thead.innerHTML = '<tr>' +  '<th>NAME AND LAST NAME</th>' + '<th>JOB</th>' + '<th>CITY</th>' + '<th>AGE</th>' +
                       '<th>DATE</th>' + '<th>SALARY</th>' + 
                    '</tr>';
  
  var tbody = table.createTBody();

  
  var tfoot = table.createTFoot();
  
  tfoot.innerHTML = '<tr><th colspan="6">OUR CUSTOMER</th></tr>';
  
  
  users.data.forEach(function(user, index){
     
    var row = table.insertRow();

    
    user.forEach(function(prop){
     
      var cell = row.insertCell();
      
      cell.innerHTML = prop;
    });
    
    tbody.append(row);
  });

  
  tableContainer.append(table);
}
JS.JSON.FetchingData.PopulateTable.ex8
--------------------------------------


A [Pen](https://codepen.io/Onlyforbopi/pen/NOWLXd) by [Pan Doul](https://codepen.io/Onlyforbopi) on [CodePen](https://codepen.io).

[License](https://codepen.io/Onlyforbopi/pen/NOWLXd/license).
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>DYNAMIC TABLE</title>
</head>
<body>

  <legend>DYNAMIC TABLE-URL JSON</legend>
  <button class="list">Display </button>  
  <div class="table-container"></div>
</body>
</html>

以上是关于html JS.JSON.FetchingData.PopulateTable.ex8的主要内容,如果未能解决你的问题,请参考以下文章

html JS.JSON.FetchingData.PopulateTable.ex4

html JS.JSON.FetchingData.PopulateTable.ex9

html JS.JSON.FetchingData.PopulateTable.ex8

html JS.JSON.FetchingData.PopulateTable.ex7

html JS.JSON.FetchingData.PopulateTable.ex6

html JS.JSON.FetchingData.PopulateTable.ex5