onlyforbopi
9/27/2018 - 7:18 AM

JS.JSON.Exercise data from this URL in a dynamic table

JS.JSON.Exercise data from this URL in a dynamic table

@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.Exercise data from this URL in a dynamic table

A Pen by Pan Doul on CodePen.

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>