-2
Hello, I have the following example array:
var users = [
{ DiaDaSemana: 'segunda a sexta', valor: 'joaozinho' },
{ DiaDaSemana: 'segunda a sexta', valor: 'mariazinha' },
{ DiaDaSemana: 'sabados', valor: 'manuel' },
{ DiaDaSemana: 'sabados', valor: 'jose' }
];
would like to know how to organize using table programmatically, with javascript in the following way:
segunda a sexta | sabados
joaozinho | manuel
mariazinha | jose
With this example I can not create neither a TH nor a TD plus lines:
<body>
<div id='myDiv'>
<table id='tabela'>
<thead id="cabecalho">
<tr id="linha">
</tr>
</thead>
</table>
</div>
<script>
var table = document.getElementById('tabela');
var thead = document.getElementById('cabecalho');
var tr = document.getElementById('linha');
var textoInserido = [];
// var existeLinha = false;
var newCell = document.createElement('th');
// Faz um loop para criar as colunas
for (var j = 0; j < thead.rows.length; j++) {
for (let indiceLinha = 0; indiceLinha < tr.cells.length; indiceLinha++) {
<% for (let i = 0; i < users.length; i++) { %>
var existeLinha = tr.cells[indiceLinha].innerText.includes("<%= users[i].DiaDaSemana %>");
if(existeLinha == false) {
newCell = tr.insertCell(<% i %>);
newCell.innerText = "<%= users[i].DiaDaSemana %>";
// insere o elemento na lista
textoInserido.push("<%= users[i].DiaDaSemana %>");
}
<% } %>
}
}
thead.appendChild(tr);
</script>
I’m using the ejs to help build the table Could someone please shed some light on this?
If you’ve tried N ways, then you have some code done, right? Please click [Edit] and add this code. Also read how to mount a [mcve]
– hkotsubo
@hkotsubo inserted the code snippet that does not work. OK ?
– Ediberto
@Ediberto checks if my answer helps you
– clone por
@clone where?
– Ediberto
checks the post
– clone por