Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

125
Visualizações
I have a program that reads data and makes it into a table. But the header won't stick?

I've made a program that reads the variable "data" and puts all the values on a table. Everything is working great except for the header. I call .table-sticky when I define myTable in the tag, but the header won't stick. Does anybody know why?

.table-sticky-container {
  top: 0;
  position: absolute;
  height: 200px;
  width: 400px;
  margin-top: 410px;
  margin-left: 803px;
  overflow-y: scroll;
  border: 2px solid grey;
}

.table-sticky th {
  position: -webkit-sticky;
  position: sticky;
  top: 0;
}
<link rel='stylesheet' href='https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css'>
    
<div class="table-sticky-container">
  <div id="container"></div>
    <script>
      var data = ["NuggetCraft", "8:30", "8:45", "ChickenCraft", "9:30", "9:45", "MoneyCraft", "10:30", "10:45", "SpaceCraft", "11:30", "11:45", "NachumCraft", "12:30", "12:45", "SpecialCraft", "13:30", "13:45","BinkleCraft", "14:30", "14:45", "SchnitzelCraft", "15:30", "15:45", "CoolCraft", "16:30", "16:45", "PianoCraft", "17:30", "17:45", "QuadCraft", "18:30", "18:45", "CalfCraft", "19:30", "19:45"];

      var myTable = "<table class=table table-sticky><thead class=thead-light><tr><th>Name</th><th>Time in</th><th>Time out</th></tr></thead><tbody><tr>";

      var perRow = 3;
      data.forEach((value, i) => {
        myTable += `<td>${value}</td>`;

        var next = i + 1;
        if (next % perRow == 0 && next != data.length) {
          myTable += "</tr><tr>";
        }
      });

      myTable += "</tr></tbody></table>";

      document.getElementById("container").innerHTML = myTable;
</script>
</div>

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The problem is because of how you are assigning attribute values inside the script tags i.e, without quotation marks

var myTable = "<table class=table table-sticky><thead class=thead-light><tr><th>Name</th><th>Time in</th><th>Time out</th></tr></thead><tbody><tr>";

Here the table class has two value table and table-sticky but since you have not used any quotation marks, the browser interprets those values very differently as explained here StackOverFlow Question. The second class is interpreted as another attribute instead of a value.

Do it like this,

var myTable = '<table class="table table-sticky"><thead class="thead-light"><tr><th>Name</th><th>Time in</th><th>Time out</th></tr></thead><tbody><tr>';
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda