1

I have a serious problem which I can't solve and I hope you can help me out.. So I have a table with a fixed header on vertical scroll (CSS and Javascript down below) but my Header isn't moving when I scroll horizontally..

    window.onscroll = function() {myFunction()};
                var header = document.getElementById('myHeader');
                var sticky = header.offsetTop;
                function myFunction() {
                    if (window.pageYOffset > sticky) {
                        header.classList.add('sticky');
                    } else {
                        header.classList.remove('sticky');
                    }
                }
    .sticky {
                  position: fixed;
                  top: 0;
                  z-index:1000;
                  padding-left:62px;
                  overflow-x: scroll;
                }


    
    
    <table id='table' class='display cell-border'>  
                    <thead class='head' id='myHeader'>
                        <tr>
                            <th class='noSearch'></th>
                            <th class='noSearch'></th>
                            <th>Shell ID</th>
                            <th>Package ID</th>
                            <th>Package Title</th>
                            <th>Product Type</th>
                            <th>Project</th>
                            <th>Current Phase</th>
                            <th>Status</th>
                        </tr>
</thead>
    <tr>
    <td>Shell ID</td>
                            <td>Package ID</td>
                            <td>Package Title</td>
                            <td>Product Type</td>
                            <td>Project</td>
                            <td>Current Phase</td>
                            <td>Status</td>
                    </tr>
          </table>

I hope you can help me out.. Cheers

Mert Silva
  • 29
  • 6

3 Answers3

0

Try this method: https://codepen.io/anon/pen/vVGeVM

<!------------------------------Css-------------------->
.tableContainer{
  overflow: auto;
  height:100px;
}
td, th{
  width:80px;
  text-align:center;
}
<!------------------------html-------------------------->

<table id='table' class='display cell-border'>  
    <thead class='head' id='myHeader'>
            <tr>
                <th class='noSearch'></th>
                <th class='noSearch'></th>
                <th>Shell ID</th>
                <th>Package ID</th>
                <th>Package Title</th>
                <th>Product Type</th>
                <th>Project</th>
                <th>Current Phase</th>
                <th>Status</th>
             <tr>
 </thead>
 <tbody>
   <tr>
    <td colspan="12">
      <div class="tableContainer">
        <table>
          <tr>
            <td>test</td>
             <td>test</td>
             <td>test</td>
             <td>test</td>
             <td>test</td>
             <td>test</td>
             <td>test</td>
             <td>test</td>
             <td>test</td>
          </tr>
        </table>
      </div>
     </td>
   </tr>
 </tbody>
</table>
SIL0RAK
  • 7
  • 2
  • 4
-2

Please use fixed header dataTable it will fix your problem. https://datatables.net/extensions/fixedheader/examples/options/simple.html

Subodh Sharma
  • 303
  • 3
  • 9
-2

according to you problem it seems that you should not use window.onScroll event left this behaviour upon css. it can handle it.

   .sticky {
          position: fixed;
          top: 0;
          z-index:100;
        }

and put this property in headers parent element

 #table{
 overflow: auto; 
}
  • Tried it, doesn't work... There's a scrollbar now underneeth my Header, which I don't want to see (just the scrollbar under my table is necessary). – Mert Silva Oct 04 '18 at 07:50