Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

120
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda