var users;
var usersDiv;
var table;
window.onload = init;
function init(){
usersDiv = document.querySelector("#container");
usersDiv.innerHTML = "";
table = document.createElement("table");
table.setAttribute("id", "table");
}
function search() {
var queryURL = "https://gist.githubusercontent.com/heiswayi/7fde241975ed8a80535a/raw/ff1caaeaf62bd6740ab7cafcd61f1215de173379/datatables-data.json";
var xhr = new XMLHttpRequest();
xhr.open('GET', queryURL, true);
xhr.onload = function(e) {
var jsonResponse = xhr.response;
users = JSON.parse(jsonResponse);
displayUsersAsATable();
}
xhr.onerror = function(err) {
console.log("Error: " + err);
}
xhr.send();
}
function displayUsersAsATable() {
createTHead();
createTableBady();
}
function createTHead(){
var row = table.insertRow();
row.insertCell().outerHTML = "<th>No</th>";
row.insertCell().outerHTML = "<th>Name</th>";
row.insertCell().outerHTML = "<th>Position</th>";
row.insertCell().outerHTML = "<th>Location</th>";
row.insertCell().outerHTML = "<th>Age</th>";
row.insertCell().outerHTML = "<th>Date of employment</th>";
row.insertCell().outerHTML = "<th>Salary</th>";
}
function createTableBady(){
var i = 1;
users.data.forEach(function(currentUser) {
var row = table.insertRow();
var numberCell = row.insertCell().innerHTML = i;
i++;
var nameCell = row.insertCell().innerHTML = currentUser[0];
var jobPositionCell = row.insertCell().innerHTML = currentUser[1];
var location = row.insertCell().innerHTML = currentUser[2];
var age = row.insertCell().innerHTML = currentUser[3];
var dateOfEmplayment = row.insertCell().innerHTML = currentUser[4];
var salary = row.insertCell().innerHTML = currentUser[5];
});
usersDiv.appendChild(table);
}
JS.JSON.Dynamic table /EdX's JavaScript Introduction Course by W3Cx/
---------------------------------------------------------------------
A [Pen](https://codepen.io/Onlyforbopi/pen/KGPmdb) by [Pan Doul](https://codepen.io/Onlyforbopi) on [CodePen](https://codepen.io).
[License](https://codepen.io/Onlyforbopi/pen/KGPmdb/license).