Skip to content

Instantly share code, notes, and snippets.

@oraricha
Created October 28, 2013 17:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oraricha/7200755 to your computer and use it in GitHub Desktop.
Save oraricha/7200755 to your computer and use it in GitHub Desktop.
Sticky thead for table Headers - a jquery plugin
/*
Massively Improved, Fixed & Documented by @OrAricha
Based on http://web-gladiator.com/stickythead/Index.html
@author: Konstantin Meiklyar - 2013
http://web-gladiator.com/stickythead/Index.html
dependancies: jquery
@param wrapper - A jquery object of the table container (preferred) *OR* any css selector representing you table container
@param height - the hight you want your container to have
*/
(function ($){
$.fn.stickythead = function(wrapper,height)
{
// Check if wrapper is already a jquery object (preferred). If not, get it by it's class name or id (preferred)
wrapper = wrapper.jquery ? wrapper : $(wrapper);
var headerTable = wrapper.clone();
var bodyTable = wrapper.clone();
headerTable.find('table').addClass("stickythead-header stickythead-table");
headerTable.find('tbody').remove();
bodyTable.find('table').addClass("stickythead-body stickythead-table");
bodyTable.find('thead').remove();
wrapper.empty()
.append("<div class='stickytrhead-header-wrapper'>" + headerTable.html() + "</div>")
.append("<div class='stickythead-body-wrapper'>" + bodyTable.html() + "</div>");
$(".stickythead-body-wrapper").css("max-height",height + "px");
wrapper.find(".stickythead-body")
.find("tr:first")
.find("td").each(function() {
var el = $(this);
var width = el.width();
var index = el.index();
var respectiveHeaderCell = wrapper.find(".stickythead-header").find('tr:first').find("td:nth-child(" + (index + 1) + ")");
respectiveHeaderCell.width(width);
// el.width(width);
});
};
}(jQuery));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment