简体   繁体   中英

make html table <thead> fixed while scroll in dynamic <tbody> in php

I have dynamic table in php where <tbody> content dynamically fetched from database, i want to make <thead> fixed while scroll, my code work fine in static html <tbody> .

Here is my code

<div class="data">
  <table>
      <thead>
        <tr>
          <th>Page Id</th>
          <th>User Id</th>
          <th>Page Title</th>
        </tr>
      </thead>
      <?php
        foreach($this->analysisL as $key => $value) {?>
      <tbody>
        <tr>
          <td><?php echo $value['daily_clicks']?></td>
          <td><?php echo $value['RDCL']?></td>
          <td><?php echo $value['SDCL']?></td>

        </tr>
      </tbody>
      <?php }?>
    </table>
  </div>

Here is my css

body{
font-family: sans-serif;
}

h1{
font-size: 2em;
}

p{
margin-top: 1em;
}
div{
padding: 0 50px
}

.data{
position: fixed;
top: 0;  
z-index: 0;
}
.content{
position: relative;
background: #fff; 
z-index: 1;
padding: 50px;
}

th,td{
width: 200px;
padding: 3px;
}

th{
background: #ccc;
}

My Goal is to make table head fixed position while scroll and the content of table body dynamic fetching from database using php loop

我建议您使用“ head fixer” js库( https://github.com/lai32290/TableHeadFixer )快速简便地固定桌子的头/脚。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM