简体   繁体   English

动态表并保持顶部固定

[英]dynamic table and keep the thead fixed

I have a dynamic table and I want to keep its head fixed when I scroll the table.我有一个动态表格,我想在滚动表格时保持其头部固定。 The table is as follows.What this basically does is, javascript will use the number in the input field and create any number of rows as it includes.I have used a fixed height for the table.But when the table grows more than the height given then I have to scroll to go down.Then the thead also goes up and go invisible, so any method to fix the thead is appreciated.表格如下。这基本上是,javascript将使用输入字段中的数字并创建任意数量的行,因为它包括。我为表格使用了固定高度。但是当表格增长超过高度时给定然后我必须滚动才能向下。然后 thead 也会上升并变得不可见,所以任何修复 thead 的方法都值得赞赏。

Note : I have gone through almost all the stuff here related to the same topic, but it doesnot seem to work for me.注意:我已经浏览了几乎所有与同一主题相关的内容,但它似乎对我不起作用。

HTML HTML

<div class="container-fluid"" style="width: 90%" style="height: 90%">
  <table id="tableAddResults" class="table table-hover" cellspacing="0">
    <thead>
      <tr>
        <th scope="col">Index No</th>
        <th scope="col">Correct A</th>
        <th scope="col">Incorrect A</th>
        <th scope="col">Total A</th>
      </tr>
    </thead>
    <tbody>
    </tbody>
  </table>

JAVASCRIPT爪哇脚本

function addRow(){

var rowcount = document.getElementById('rowcount').value;
var table = document.getElementById('tableAddResults');

for(y=0;y<rowcount;y++){
    var newrow = table.insertRow();

    var cell0 = newrow.insertCell(0);
    var cell0Text = document.createTextNode('AT-');
    cell0.appendChild(cell0Text);
    cell0.setAttribute('contentEditable','true');

    for(i=1;i<4;i++){
        var cell = newrow.insertCell(i);
        var cellText = document.createTextNode('');
        cell.appendChild(cellText);
        cell.setAttribute('contentEditable','true');
    }
}
};

The CSS code I used is given below.我使用的 CSS 代码如下。

div.container-fluid{
   overflow:hidden;
   overflow-y: scroll;
   height: 450px;

} }

You can do this purly with css using position: sticky like the following.您可以使用 css 使用position:sticky纯粹地做到这一点,如下所示。 You can then change top to tell it how far it needs to be from the top to start becoming sticky.然后,您可以更改top以告诉它需要离顶部多远才能开始变粘。

A stickily positioned element is an element whose computed position value is sticky.粘性定位元素是其计算出的位置值具有粘性的元素。 It's treated as relatively positioned until its containing block crosses a specified threshold (such as setting top to value other than auto) within its flow root (or the container it scrolls within), at which point it is treated as "stuck" until meeting the opposite edge of its containing block.它被视为相对定位,直到它的包含块在其流根(或其滚动的容器)内超过指定的阈值(例如将 top 设置为 auto 以外的值),此时它被视为“卡住”,直到满足其包含块的相对边缘。

 thead > tr > th { position: sticky; top: 0; background: white; } table {width: 100%;}
 <div class="container-fluid" style="width: 90%" style="height: 90%"> <table id="tableAddResults" class="table table-hover" cellspacing="0"> <thead> <tr> <th scope="col">Index No</th> <th scope="col">Correct A</th> <th scope="col">Incorrect A</th> <th scope="col">Total A</th> </tr> </thead> <tbody> <tr><td>test</td><td>test</td><td>test</td><td>test</td></tr> <tr><td>test</td><td>test</td><td>test</td><td>test</td></tr> <tr><td>test</td><td>test</td><td>test</td><td>test</td></tr> <tr><td>test</td><td>test</td><td>test</td><td>test</td></tr> <tr><td>test</td><td>test</td><td>test</td><td>test</td></tr> <tr><td>test</td><td>test</td><td>test</td><td>test</td></tr> <tr><td>test</td><td>test</td><td>test</td><td>test</td></tr> <tr><td>test</td><td>test</td><td>test</td><td>test</td></tr> <tr><td>test</td><td>test</td><td>test</td><td>test</td></tr> <tr><td>test</td><td>test</td><td>test</td><td>test</td></tr> <tr><td>test</td><td>test</td><td>test</td><td>test</td></tr> <tr><td>test</td><td>test</td><td>test</td><td>test</td></tr> <tr><td>test</td><td>test</td><td>test</td><td>test</td></tr> <tr><td>test</td><td>test</td><td>test</td><td>test</td></tr> <tr><td>test</td><td>test</td><td>test</td><td>test</td></tr> <tr><td>test</td><td>test</td><td>test</td><td>test</td></tr> <tr><td>test</td><td>test</td><td>test</td><td>test</td></tr> <tr><td>test</td><td>test</td><td>test</td><td>test</td></tr> <tr><td>test</td><td>test</td><td>test</td><td>test</td></tr> <tr><td>test</td><td>test</td><td>test</td><td>test</td></tr> <tr><td>test</td><td>test</td><td>test</td><td>test</td></tr> <tr><td>test</td><td>test</td><td>test</td><td>test</td></tr> <tr><td>test</td><td>test</td><td>test</td><td>test</td></tr> <tr><td>test</td><td>test</td><td>test</td><td>test</td></tr> <tr><td>test</td><td>test</td><td>test</td><td>test</td></tr> </tbody> </table> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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