修复了具有rowspan和colspan属性且可滚动的表标题

fix table header with rowspan and colspan property and scrollable

本文关键字:滚动 标题 属性 colspan rowspan      更新时间:2023-09-26

我想将表头固定在一个位置,并允许表的其余内容滚动(下面的代码)。我在表格标题行中使用colspan和rowsapn属性。有什么办法解决它吗?

<html>
   <body> 
   <div id="secondtable" style="width:100%; height:380px; overflow:auto;">
   <table class="mytable" style="width:100%; border-width:thin;" border="1"      cellpadding="9" cellspacing="0" align="center">    
                <tr class="tt" bgcolor="#0B2B62">
                    <th>No.</th>
                    <th>Store Name</th>
                    <th>Receipts</th>
                    <th colspan=2>Receipts vs Budget</th>
                    <th colspan=3>Receipts vs Last Year</th>
                    <th>Contribution</th>
                    <th colspan=2>Contribution vs Budget</th>
                </tr>
                <tr class="tt" style="background-color:#C2C2C2;">
                    <td></td>
                    <td></td>
                    <td></td>
                    <td><font color="black">Var &pound;</font></td>
                    <td><font color="black">Var %</td>
                    <td><font color="black">Var &pound;</font></td>
                    <td><font color="black">Var %</font></td>
                    <td><font color="black">Var</font></td>
                    <td></td>
                    <td><font color="black">Var &pound;</font></td>
                    <td><font color="black">Var %</font></td>
                </tr>
                <tr class="tt" style="background-color:#D7F2FF">
                    <td>&nbsp;</td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td></td>
                </tr>
        </table>
</div>
</body>
</html>

//这是css

table {
border-collapse:separate;
border-spacing: 0;
color:white;
background-color:white;
border: 1px solid black;
border-radius: 8px;
-moz-border-radius: 5px;
padding: 5px;
font-family:sans-serif;
}
.mytable{
background-color:#014483;
}
.tt{
border-color:#2B4F81;
}
.ttt{
background-color:#BBDBF4;
border:#2B4F81;
}

3岁的问题,但是对于那些仍然没有解决方案的人(带有rowspan的表头),我终于有了使用js的方法。

<div id="table">
 <table>
   <thead>
     <!-- your head code -->
   </thead>
   <tbody>
     <!-- your body code -->
   </body>
 </table>
</div>

CSS

#table{
    height:200px; //for example 
    overflow-y:auto; 
}

JS-

let statusCard = document.querySelector('#table');
    // add scroll event listener for change head's position 
    statusCard.addEventListener('scroll',e =>{
      let tableHead = document.querySelector('thead');
      let scrollTop =  statusCard.scrollTop;
      tableHead.style.transform = 'translateY(' + scrollTop + 'px)';
})

您可能需要将头部放置在具有类.sticky或任何您喜欢的类以及适当CSS的div中。

HTML

  <div class="sticky">
  <table class="mytable" style="width:100%; border-width:thin;" border="1" cellpadding="9" cellspacing="0" align="center">
      <tr class="tt" bgcolor="#0B2B62">
        <th>No.</th>
        <th>Store Name</th>
        <th>Receipts</th>
        <th colspan=2>Receipts vs Budget</th>
        <th colspan=3>Receipts vs Last Year</th>
        <th>Contribution</th>
        <th colspan=2>Contribution vs Budget</th>
      </tr>
  </table>
  </div>

CSS

.sticky {
  position: fixed;
  top: 2px;
  width: 96.5%;
}

请参阅此处的示例:http://codepen.io/omerblink/pen/wMzLow?editors=110

您可以使用:

.mytable tr th
{
    table-layout: fixed;
}

这可能会对您有所帮助:

html, body{
  margin:0;
  padding:0;
  height:100%;
}
section {
  position: relative;
  border: 1px solid #000;
  padding-top: 37px;
  background: #500;
}
section.positioned {
  position: absolute;
  top:100px;
  left:100px;
  width:800px;
  box-shadow: 0 0 15px #333;
}
.container {
  overflow-y: auto;
  height: 200px;
}
table {
  border-spacing: 0;
  width:100%;
}
td + td {
  border-left:1px solid #eee;
}
td, th {
  border-bottom:1px solid #eee;
  background: #ddd;
  color: #000;
  padding: 10px 25px;
}
th {
  height: 0;
  line-height: 0;
  padding-top: 0;
  padding-bottom: 0;
  color: transparent;
  border: none;
  white-space: nowrap;
}
th div{
  position: absolute;
  background: transparent;
  color: #fff;
  padding: 9px 25px;
  top: 0;
  margin-left: -25px;
  line-height: normal;
  border-left: 1px solid #800;
}
th:first-child div{
  border: none;
}

我怀疑您是否能够用一个表实现这一点。所以我实际上已经创建了两个表,一个用于header,另一个用于body&前者总是固定不变的。我还假设列的数量是静态的。这是代码

<!--Header part-->    
<div id="firsttable" style="width:100%;">
        <table class="mytable" style="width:100%; border-width:thin;" border="1" cellpadding="9" cellspacing="0" align="center">
          <thead><tr class="tt " bgcolor="#0B2B62">
            <th id="no">No.</th>
            <th id="sto">Store Name</th>
            <th id="res">Receipts</th>
            <th colspan=2 id="rb">Receipts vs Budget</th>
            <th colspan=3 id="rl">Receipts vs Last Year</th>
            <th id="con">Contribution</th>
            <th colspan=2 id="cb">Contribution vs Budget</th>
          </tr>
          </thead>
          </table>
          </div>
<!-- body part -->
    <div id="secondtable" style="width:100%; height:380px; overflow:auto;">
              <table class="mytable" style="width:100%; border-width:thin;" border="1" cellpadding="9" cellspacing="0" align="center">
              <tr class="tt" style="background-color:#C2C2C2;">
                <td headers="no">A</td>
                <td headers="sto">B</td>
                <td headers="res">C</td>
                <td colspan="2" headers="rb"><div style="color:black">Var &pound;</div><div style="color:black">Var %</div></td>
                <td colspan="3"><div style="color:black">Var &pound;</div><div>Var %</div><div>Var</div></td>
                <td>D</td>
                <td colspan="2"><div style ="color:black">Var &pound;</div><div style="color:black">Var %</div></td>
              </tr>
              <!-- Include multiple tr to get scrollbar -->
            </table>
          </div>

CSS

table {
border-collapse:separate;
border-spacing: 0;
color:white;
background-color:white;
border: 1px solid black;
border-radius: 8px;
-moz-border-radius: 5px;
padding: 5px;
font-family:sans-serif;
}
.mytable{
background-color:#014483;
}
.tt{
border-color:#2B4F81;
}
.ttt{
background-color:#BBDBF4;
border:#2B4F81;
}
td,th{
    max-width:10px;
    }
.mytable div{
    max-width:40%;
    display:inline
    }

工作副本

注意:当此滚动条可见时,您可能会看到标题列与正文列不同步。这是因为滚动条有一个宽度,它会占用一些空间。在这种情况下,您可以调整任何正文列的宽度,使其同步。我也知道,基本上你需要7列——可以有子列。因此,在正文中,我创建了7个列和div来复制子列

希望这对有帮助