15

I'm trying to figure out how to keep the table head visible when scrolling. Is there a setting in semantic ui for this? Or will I just have to use a non-semantic ui solution?

You'll need to view "Full page" to see the table correctly.

<script src="https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/1.12.0/semantic.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/1.12.0/semantic.css" rel="stylesheet" />

<div style="height:200px;overflow:auto">
  <table class="ui small celled striped table" sytle="width:100%">
    <thead>
      <tr>
        <th>Date</th>
        <th>Status</th>
        <th>Facility Name</th>
        <th>Phone</th>
      </tr>
    </thead>
    <tbody data-bind="foreach:FollowupEntries">
      <tr>
        <td>Date</td>
        <td>Status</td>
        <td>Facility Name</td>
        <td>Phone</td>
      </tr>
      <tr>
        <td>Date</td>
        <td>Status</td>
        <td>Facility Name</td>
        <td>Phone</td>
      </tr>
      <tr>
        <td>Date</td>
        <td>Status</td>
        <td>Facility Name</td>
        <td>Phone</td>
      </tr>
      <tr>
        <td>Date</td>
        <td>Status</td>
        <td>Facility Name</td>
        <td>Phone</td>
      </tr>
      <tr>
        <td>Date</td>
        <td>Status</td>
        <td>Facility Name</td>
        <td>Phone</td>
      </tr>
      <tr>
        <td>Date</td>
        <td>Status</td>
        <td>Facility Name</td>
        <td>Phone</td>
      </tr>
      <tr>
        <td>Date</td>
        <td>Status</td>
        <td>Facility Name</td>
        <td>Phone</td>
      </tr>
      <tr>
        <td>Date</td>
        <td>Status</td>
        <td>Facility Name</td>
        <td>Phone</td>
      </tr>
    </tbody>
  </table>
</div>
tcigrand
  • 2,357
  • 2
  • 14
  • 24
  • 2
    This is currently not built into Semantic-UI but is something that has been requested. https://github.com/Semantic-Org/Semantic-UI/issues/1357 – Stewartside Apr 27 '15 at 21:30
  • Ok, thanks. I'll keep an eye on that issue. – tcigrand Apr 27 '15 at 21:33
  • Any update about this? Have you found an solution? I am running into the same problem. – P Griep Oct 15 '15 at 11:01
  • 1
    @PGriep - no update, still not supported by Semantic UI, but the author says it's on the roadmap – tcigrand Oct 21 '15 at 22:37
  • @tcigrand thank you for your feedback. I've made my own solution with 2 tables at this point and will wait before Semantic UI supports it. – P Griep Oct 23 '15 at 06:51
  • same issue resolved with this stack http://stackoverflow.com/questions/17067294/html-table-with-100-width-with-vertical-scroll-inside-tbody – user6742685 Aug 22 '16 at 08:35

6 Answers6

32

I solved the problem with position: sticky, like this:

.ui.table thead tr:first-child > th {
     position: sticky !important;
     top: 0;
     z-index: 2;
}
Ashkan
  • 846
  • 11
  • 20
  • 2
    This worked well for me. I took first-child off as I had multiple header rows and worked like a charm – alex bennett Dec 14 '18 at 20:40
  • 1
    This worked for me aswell, just remember to set a max-height for your table and overflow auto – Chagall Feb 04 '19 at 19:27
  • 2
    I also added `.ui.table { border-top: none !important; } th, tr, thead { box-sizing: border-box; }` to remove some position flicker when scrolling. – eskimwier May 23 '19 at 14:45
  • 1
    great solution! been looking for answers and trying to hack it for quite some time now. – user1189352 May 29 '20 at 20:37
1

As @Stewartside suggested, this isn't current built into Semantic UI, but it has been discussed.

tcigrand
  • 2,357
  • 2
  • 14
  • 24
0

Though I don't recommend it if you really really want it to work even with hacks this should work for you:

<table class="semantic's class" style="margin-bottom:0px;border-bottom:none">
   <thead>...</thead>
</table>
<table class="semantic's class" style="margin-top:0px; border-top: none">
  <div style="overflow-y:scroll; height: YOUR-REQUIRED-HEIGHT">
    <thead style="visible:hidden">...</thead>
    <tbody>...</tbody>
  </div>
</table>
0

This script will probably do the job for you. Just add the class "sticky" to your table tag and adjust the offset from the top:

$(document).ready(function(){
    var tableTop = $('.sticky.table').offset().top;

    $('.sticky.table').children('thead').children('tr').children('th').each( function()  { 
        $(this).css({  width: $(this).outerWidth() }); 
    });

    $(window).scroll(function() {
        var fromTop = $(window).scrollTop(); 

        if($('.sticky.table').length > 0){
            stickyTableHead(fromTop);
        }
    });

    function stickyTableHead(fromTop){
        if(fromTop > tableTop ){
            $('.sticky.table').children('thead').css({'position': 'fixed', top: 0 }); 
        }else{
            $('.sticky.table').children('thead').css({'position': 'relative', top: 0});
        }
    };
});
McCygnus
  • 4,187
  • 6
  • 34
  • 30
0

Applying Ashkan's solution to any table.

table{
  width:100%;
}
table,th,td{
  border: 1px solid grey;
  border-collapse: collapse;
}
thead {
  background-color: grey;
  position: sticky !important;
  top: 0;
  z-index: 2;
}
<div style="overflow: auto; height:100px;">
  <table>
    <thead>
      <tr>
        <th>ID</th>
        <th>ONE</th>
        <th>TWO</th>
        <th>THREE</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>1</td><td>A</td><td>B</td><td>C</td>
      </tr>
      <tr>
        <td>2</td><td>D</td><td>E</td><td>F</td>
      </tr>
      <tr>
        <td>3</td><td>G</td><td>H</td><td>I</td>
      </tr>
      <tr>
        <td>4</td><td>J</td><td>K</td><td>L</td>
      </tr>
      <tr>
        <td>5</td><td>M</td><td>N</td><td>O</td>
      </tr>
      <tr>
        <td>6</td><td>P</td><td>Q</td><td>R</td>
      </tr>
      <tr>
        <td>7</td><td>S</td><td>T</td><td>U</td>
      </tr>
      <tr>
        <td>8</td><td>V</td><td>W</td><td>X</td>
      </tr>
      <tr>
        <td>9</td><td>Y</td><td>Z</td><td>0</td>
      </tr>
    </tbody>
  </table>
</div>
DeFRaNDo
  • 11
  • 2
-2

Check out this JSFiddle, I think it is the kind of thing you're looking for.. specifically check out the CSS for the thead tag.

thead { 
position: fixed;
top: 0;
left: 0;
background-color: white;
}
Jacob Calvert
  • 188
  • 1
  • 2
  • 11
  • Thanks for the suggestion, but I'm looking for something in Semantic UI. Also, I wasn't clear, but there are other items above this table and I don't want to use fixed positioning. – tcigrand Apr 27 '15 at 21:33