Published
Edited
Feb 2, 2019
3 forks
6 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
table1 = {
// Table data -- an array of objects
var jsData = new Array( );
jsData[0] = {location:"Uruguay", year:1930, winner:"Uruguay", winScore:4,
loser:"Argentina", losScore:2};
jsData[1] = {location:"Italy", year:1934, winner:"Italy", winScore:2,
loser:"Czechoslovakia", losScore:1};
jsData[2] = {location:"France", year:1938, winner:"Italy", winScore:4,
loser:"Hungary", losScore:2};
jsData[3] = {location:"Brazil", year:1950, winner:"Uruguay", winScore:2,
loser:"Brazil", losScore:1};
jsData[4] = {location:"Switzerland", year:1954, winner:"West Germany", winScore:3,
loser:"Hungary", losScore:2};
// Draw table from 'jsData' array of objects
function drawTable(tbody) {
var tr, td;
tbody = matchData.querySelector('tbody');
// loop through data source
for (var i = 0; i < jsData.length; i++) {
tr = tbody.insertRow(tbody.rows.length);
td = tr.insertCell(tr.cells.length);
td.setAttribute("align", "center");
td.innerHTML = jsData[i].year;
td = tr.insertCell(tr.cells.length);
td.innerHTML = jsData[i].location;
td = tr.insertCell(tr.cells.length);
td.innerHTML = jsData[i].winner;
td = tr.insertCell(tr.cells.length);
td.innerHTML = jsData[i].loser;
td = tr.insertCell(tr.cells.length);
td.setAttribute("align", "center");
td.innerHTML = jsData[i].winScore + " - " + jsData[i].losScore;
}
}
drawTable("matchData");
}
Insert cell
Insert cell
Insert cell
matchData_fiddle = html`<table id="cupFinals" border=1 px>
<tbody></tbody>
</table>`
Insert cell
{
// Draw table from 'jsData' array of objects
function drawTable(tbody) {
var tbody = matchData_fiddle.querySelector('tbody');
var headerNames = Object.getOwnPropertyNames(jsData_as_JSON[0]);
var columnCount = headerNames.length;
//Add the header row.
var row = tbody.insertRow(-1);
for (var i = 0; i < columnCount; i++) {
var headerCell = document.createElement("TH");
headerCell.innerHTML = headerNames[i];
row.appendChild(headerCell);
}

// loop through data source
for (var i = 0; i < jsData_as_JSON.length; i++) {
var tr = document.createElement("tr");
for(var colName in jsData_as_JSON[i]) {
var td = document.createElement("td");
td.innerHTML = jsData_as_JSON[i][colName];
if(colName=="Location")
td.className = "centered";
tr.appendChild(td);
}
tbody.appendChild(tr);
}
}
drawTable("matchData_fiddle");
}
Insert cell
Insert cell
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more