Basically I'm creating a dynamic table on javascript and displaying it in the html page. I also have a css file that works on static elements, but not on the table i'm creating dynamically. what can I do to style my dynamically created table?
I've tried this: table.classList.add('tablestyle'); but it doesn't work.
As you can imagne, I'm absolutly new to this kind of things.
I have 3 files:
reports.component.ts (that contains html. references to sccs & js are in the file and they work correctly)
<html>
<div class="block">
<h1>STORICO DEI DATI</h1>
<div id="myDynamicTable" class="table">
<body onload="Javascript:addTable()"></body>
</div>
</div>
</html>
reports.component.js
function addTable() {
let colonne = Math.floor(Math.random() * 5)+2;
let righe = Math.floor(Math.random() * 5)+2;
var today = new Date();
var dd = String(today.getDate()).padStart(2, '0');
var mm = String(today.getMonth() + 1).padStart(2, '0'); //January is 0!
var yyyy = today.getFullYear();
today = mm + '/' + dd + '/' + yyyy;
let providers = ["p1", "p2", "p3", "np4", "p5"];
let testcases = ["tc1", "tc2", "tc3", "tc4", "tc5"];
var myTableDiv = document.getElementById("myDynamicTable");
var table = document.createElement('table');
table.classList.add('tablestyle');
var tableBody = document.createElement('TBODY');
table.appendChild(tableBody);
for (var i=0; i<righe; i++){
var tr = document.createElement('tr');
tr.style.backgroundColor = 'red';
tableBody.appendChild(tr);
for (var j=0; j<colonne; j++){
var td = document.createElement('td');
td.width='75';
if(i==0){
if(j==0){ //prima casella
addCell(td, tr, today);
}
else { //prima riga
addCell(td, tr, providers[j-1]);
}
}
else {
if(j==0){ //prima colonna
addCell(td, tr, testcases[i-1]);
}
else {
addCell(td, tr, Math.floor(Math.random() * 50));
}
}
}
}
myTableDiv.appendChild(table);
}
function addCell(td, tr, valoreCella){
td.appendChild(document.createTextNode(valoreCella));
tr.appendChild(td);
}
report.component.scss (longer, but this is the part i want to work)
.tablestyle{
font-weight: bold;
}
table.classList.add('tablestyle')itself is correct and should work, unless old browser.bodytag is at the wrong spot.bodytag which holds youronload.