HTML Fragments + JS Kernel
Outputting a table using Javascript Kernel
function logIt(output) {
console.log(output);
}
logIt("Hello everyone!");
function logt(output) {
console.log(output);
}
// calls to the function
logt("test test");
// define a function to hold data for a Person
function Person(name, nationality, age) {
this.name = name;
this.nationality = nationality;
this.age = age;
this.role = "";
}
// define a setter for role in Person data
Person.prototype.setRole = function(role) {
this.role = role;
}
// defines the manager
var manager = new Person("Zidane", "France", 50);
manager.setRole("Manager");
// JSON conversion from object to a string
Person.prototype.toJSON = function() {
const obj = {name: this.name, nationality: this.nationality, age: this.age, role: this.role};
const json = JSON.stringify(obj);
return json;
}
// player data
var players = [
new Person("Kevin de Bruyne", "Belgium", 31),
new Person("Lionel Messi", "Argentina", 35),
new Person("Vinicius Jr.", "Brazil", 22),
new Person("Neymar", "Brazil", 30),
new Person("Haaland", "Norway", 22),
new Person("Sergio Ramos", "Spain", 36),
];
function Squad(manager, players){
this.manager = manager;
this.squad = [manager];
this.players = players;
this.players.forEach(player => {player.setRole("Player"); this.squad.push(player); });
this.json = [];
this.squad.forEach(player => this.json.push(player.toJSON()));
}
var FUT = new Squad(manager, players);
Squad.prototype._toHtml = function() {
var style = (
"display:inline-block;" +
"background:black;" +
"border: 2px solid grey;" +
"box-shadow: 0.8em 0.4em 0.4em grey;"
);
// HTML Body of Table is build as a series of concatenations (+=)
var body = "";
// Heading for Array Columns
body += "<tr>";
body += "<th><mark>" + "Name" + "</mark></th>";
body += "<th><mark>" + "Nationality" + "</mark></th>";
body += "<th><mark>" + "Age" + "</mark></th>";
body += "<th><mark>" + "Role" + "</mark></th>";
body += "</tr>";
// Data of Array, iterate through each row of lakers team
for (var row of FUT.squad) {
// tr for each row, a new line
body += "<tr>";
// td for each column of data
body += "<td>" + row.name + "</td>";
body += "<td>" + row.nationality + "</td>";
body += "<td>" + row.age + "</td>";
body += "<td>" + row.role + "</td>";
// tr to end line
body += "<tr>";
}
// Build and HTML fragment of div, table, table body
return (
"<div style='" + style + "'>" +
"<table>" +
body +
"</table>" +
"</div>"
);
};
// IJavaScript HTML processor receive parameter of defined HTML fragment
$$.html(FUT._toHtml());