You are viewing limited content. For full access, please sign in.

Question

Question

scroll table

asked on February 3, 2016 Show version history

Hi,

I was able to make a table scroll by this simple CSS

.itemTable {height: 320px; overflow-y: auto;}

I have attached the screenshot. The table class is itemTable. The problem is it scrolls together with the column headers. I need help to make the column headers static. Thanks 

Regards,

Mark

scroll.png
scroll.png (19.15 KB)
0 0

Replies

replied on February 3, 2016

Try the suggestion from this stackoverflow thread.

For your CSS

thead, tbody { display: block; }

tbody {
    height: 100px;       /* Just for the demo          */
    overflow-y: auto;    /* Trigger vertical scroll    */
    overflow-x: hidden;  /* Hide the horizontal scroll */
}

And for your JS

$(document).ready(function () {
  
  // Change the selector if needed
var $table = $('table.itemTable'),
    $bodyCells = $table.find('tbody tr:first').children(),
    colWidth;

// Adjust the width of thead cells when window resizes
$(window).resize(function() {
    // Get the tbody columns width array
    colWidth = $bodyCells.map(function() {
        return $(this).width();
    }).get();
    
    // Set the width of thead columns
    $table.find('thead tr').children().each(function(i, v) {
        $(v).width(colWidth[i]);
    });    
}).resize(); // Trigger resize handler
  
});

Your table can keep the class name itemTable.

0 0
replied on February 23, 2016

Hi Alex, 

Thanks for the solution it worked quite well. The only down side I got was when I used it on tow tables. Before using your solution I gave two tables a similar class, e.g. "linked", and used the following code

$('.linked').scroll(function(){
  $('.linked').scrollTop($(this).scrollTop());   

So both scrolled at the same time so that both would display the same row. Now the code will not work due to the static column header. Would you have a solution for that?

Regards

Mark

0 0
replied on January 5, 2023

I tried this code but I get this result with the Column Labels

I tried the Row Widths set Auto and by setting the width values and see the same results.

Thoughts?

0 0
You are not allowed to follow up in this post.

Sign in to reply to this post.