动态表头修复问题

时间:2014-07-16 04:50:31

标签: jquery html css

我这里有一个完全动态的表,所有单元格都是从数据库的值动态生成的。问题是如何在滚动x和y轴时修复我的表头始终可见。我只是粘贴静态代码和我的小提琴。 http://jsfiddle.net/kannankds/8gMVJ/5/

<div id="container">
<table id="my_table">
<thead>
<th>head1</th>
<th>head1</th>
</thead>
<tbody>
<tr><td>data1</td>
<td>data2</td>
</tr>
</tbody>
</table>
</div>

2 个答案:

答案 0 :(得分:1)

您可以使用某些css执行此操作:

#table-wrapper {
  position:relative;
}
#table-scroll {
  height:150px;
  overflow:auto;  
  margin-top:20px;
}
#table-wrapper table {
  width:100%;

}
#table-wrapper table * {
  background:yellow;
  color:black;
}
#table-wrapper table thead th .text {
  position:absolute;   
  top:-20px;
  z-index:2;
  height:20px;
  width:35%;
  border:1px solid red;
}

HTML:

<div id="table-wrapper">
  <div id="table-scroll">
    <table>
        <thead>
            <tr>
                <th><span class="text">A</span></th>
                <th><span class="text">B</span></th>
                <th><span class="text">C</span></th>
            </tr>
        </thead>
        <tbody>
<tr> <th>1, 0</th> <th>2, 0</th> <th>3, 0</th> </tr>
<tr> <th>1, 1</th> <th>2, 1</th> <th>3, 1</th> </tr>
<tr> <th>1, 2</th> <th>2, 2</th> <th>3, 2</th> </tr>
<tr> <th>1, 3</th> <th>2, 3</th> <th>3, 3</th> </tr>
        </tbody>
    </table>
  </div>
</div>

DEMO

您也可以使用

  1. FixedHeaderPlugin
  2. http://www.imaputz.com/cssStuff/bigFourVersion.html
  3. http://imar.spaanjaars.com/357/a-scrollable-table-with-a-fixed-header
  4. http://jsfiddle.net/deepakmanwal/dBe5w/

答案 1 :(得分:0)

根据您的问题,您似乎需要修复标题/冻结列点击以下链接我希望有帮助

http://www.imaputz.com/cssStuff/bigFourVersion.html