Sticky Table Headers

你离开我真会死。 提交于 2019-12-01 07:15:22

问题


I am using this plugin to achieve a sticky Table Header in my Table. Actually as in the plugin example and in my page, the table Header disappear a bit later the last row in the table. I want my table header disappearing exactly when the last row is gone.There is a chance to achieve that?


回答1:


here's a working example: fiddle

all I changed was the end of this line:

if ((scrollTop > offset.top) && (scrollTop < offset.top + $this.height()-base.$clonedHeader.height())) {



回答2:


You could add a bug report on github:

https://github.com/jmosbech/StickyTableHeaders/issues




回答3:


I know this is old, but I felt like contributing something that may help some...

For dynamic tables (where the width for each cell is pre-computed), this plugin messes up the widths for the header row.

I made a few changes that help compute the width based on each outerWidth from the cells based on the first tbody row.

First comment out the current width calculations in the plugin:

base.updateWidth = function () {
        // Copy cell widths and classes from original header
        $('th', base.$clonedHeader).each(function (index) {
            var $this = $(this);
            var $origCell = $('th', base.$originalHeader).eq(index);
            this.className = $origCell.attr('class') || '';
            //$this.css('width', $origCell.width());
        });

        // Copy row width from whole table
        //base.$clonedHeader.css('width', base.$originalHeader.width());
    };

    // Run initializer
    base.init();

Then add the following to the end of base.toggleHeaders = function () {

// Set cell widths for header cells based on first row's cells
var firstTr = $this.children("tbody").children("tr").first();
var iCol = 0;
$(firstTr).children("td:visible").each(function() 
{
    var colWidth = $(this).outerWidth();
    console.log(colWidth.toString());
    var headerCell = $this.children("thead.tableFloatingHeader").children("tr").children("th:eq(" + iCol + ")");
    var firstRowCell = $this.children("tbody").children("tr:first").children("td:eq(" + iCol + ")");
    $(headerCell).outerWidth(colWidth);
    $(firstRowCell).outerWidth(colWidth);

    iCol++;
});



回答4:


This can be achieved with div tables.

.td.header {
  position: sticky;
  top:0px;
}

Check out this jsfiddle for simple example.




回答5:


I wrote a jquery library that makes the table be fixed at the top of the page and disappearing exactly when the last row is gone

This is a small jquery library functions combined with ScrollFix https://github.com/ShiraNai7/jquery-scrollfix library to provide a header table fixed below the main menu. the library supports several tables on the same page

https://github.com/doska80/ScrollTableFixed



来源:https://stackoverflow.com/questions/12405709/sticky-table-headers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!