Created
October 28, 2013 17:12
-
-
Save oraricha/7200755 to your computer and use it in GitHub Desktop.
Sticky thead for table Headers - a jquery plugin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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