Create dynamic JS table to use in HTML

Asked

Viewed 45,048 times

4

I’m trying to create a table in Javascript to then use it in a page, but this difficult to accomplish

The goal is that the table represents a set of hospital beds that will be managed through the HTML page.

How to do this?

  • What do you mean? you want to generate HTML table code through Javascript?

  • 2

    Post the code you already have and be more specific on what point your doubt is.

  • Good afternoon, did my answer help you? Please let me know if you still have any questions. if not and the answer was helpful, please mark as "correct". Grateful.

  • Good afternoon, I wonder if any of the answers helped you, if not please comment on what you think is missing.

2 answers

8

To create dynamic elements with Javascript, you must use document.createElement and to add to another existing html element, you must use Node.appendChild().

To create a table, you should call something like:

var novaTabela = document.createElement("table");

To add it to an existing DIV such as:

<div id="test"></div>

You to do it:

var novaTabela = document.createElement("table");
document.getElementById("test").appendChild(novaTabela);

The table usually requires (is recommended) <thead> and <tbody> (sometimes you can use <tfoot>, but is optional).

To create the <thead> and the <tbody> do this:

var tabela = document.createElement("table");
var cabecalho = document.createElement("thead");
var corpo = document.createElement("tbody");

tabela.appendChild(cabecalho);
tabela.appendChild(corpo);

document.getElementById("test").appendChild(tabela);

Note: Inside the <thead>, use <th> instead of <td>

Read about the tags:

Using the innerHTML

Some will criticize, but it doesn’t kill anyone to use the innerHTML sometimes you can try something like:

var container = document.getElementById("container");
container.innerHTML = [
  '<table>',
  '<thead>',
  '<tr>',
  '<th>id</th>',
  '<th>col1</th>',
  '<th>col2</th>',
  '<th>col3</th>',
  '</tr>',
  '</thead>',
  '<tbody>',
  '<tr>',
  '<td>1</td>',
  '<td>data</td>',
  '<td>data</td>',
  '<td>data</td>',
  '</tr>',
  '<tr>',
  '<td>2</td>',
  '<td>data</td>',
  '<td>data</td>',
  '<td>data</td>',
  '</tr>',
  '<tr>',
  '<td>3</td>',
  '<td>data</td>',
  '<td>data</td>',
  '<td>data</td>',
  '</tr>',
  '</tbody>',
  '</table>'
].join("\n");
table {
  border: 1px #c0c0c0 solid;
  width: 100%;
}
table th {
  background-color: #00f;
  color: #fff;
}
table td {
  background-color: #f0f0f0;
  color: #0c0c0c;
}
<div id="container"></div>

  • Cool the code! How can I return data in JSON and create the <td> dynamically?

5

I created this function for you, if you want to use it.

function criarTabela(conteudo) {
  var tabela = document.createElement("table");
  var thead = document.createElement("thead");
  var tbody=document.createElement("tbody");
  var thd=function(i){return (i==0)?"th":"td";};
  for (var i=0;i<conteudo.length;i++) {
    var tr = document.createElement("tr");
    for(var o=0;o<conteudo[i].length;o++){
      var t = document.createElement(thd(i));
      var texto=document.createTextNode(conteudo[i][o]);
      t.appendChild(texto);
      tr.appendChild(t);
    }
    (i==0)?thead.appendChild(tr):tbody.appendChild(tr);
  }
  tabela.appendChild(thead);
  tabela.appendChild(tbody);
  return tabela;
}
document.getElementById("tabela").appendChild(criarTabela([
  ["id", "nome",     "idade"],
  [1,    "matheus",  16],
  [2,    "cristian", 16],
  [3,    "pedro",    10],
  [4,    "henrique", 10]
]));
<div id="tabela"></div>

  • Welcome to Stackoverflow in English, Matthew thanks for the collaboration, but the goal of the community is to present "answers" to the questions, that is, your answer should be an "ANSWER" to the question and not just a sharing of code. In the tour read the part "Get answers to practical and detailed questions" and read http://answall.com/help/how-to-answer :)

  • @Guilhermenascimento I know the question is old. But I do not agree with his comment. There are many answers around here that are just codes, and in this case here, Matheus presented a functional code and according to the question.

  • @Eduardoseixas Maybe you don’t understand the purpose of the site, I’ll explain, I hope you don’t take this the wrong way. The purpose is not only to give code here on the site, but rather knowledge, a code is legal, but it must come accompanied by an explanation, in no time I said that the code is bad or does not work, ie I did not criticize the code but rather the way the answer was formulated. This thing I did is called constructive criticism, note that in my answer below I explained the functions and cited sources. I really hope you understand this as constructive criticism :)

  • @Matheus Cristin. How can I return data in JSON and create the <td> dynamically?

  • I just have to thank the posting of the Lord Matthew, because it was the only way that served to teach me how to do, Posting a functional model, short and objective. Thanks Matthew and everyone for the space available.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.