2 /*! Copyright (c) 2011 by Jonas Mosbech - https://github.com/jmosbech/StickyTableHeaders
3 MIT license info: https://github.com/jmosbech/StickyTableHeaders/blob/master/license.txt */
6 $
.StickyTableHeaders
= function (el
, options
) {
7 // To avoid scope issues, use 'base' instead of 'this'
8 // to reference this class from internal events and functions.
11 // Access to jQuery and DOM versions of element
15 // Cache DOM refs for performance reasons
16 base
.$window
= $
(window
);
17 base
.$clonedHeader
= null;
18 base
.$originalHeader
= null;
20 // Add a reverse reference to the DOM object
21 base
.$el
.data('StickyTableHeaders', base
);
23 base
.init
= function () {
24 base
.options
= $
.extend({}, $
.StickyTableHeaders
.defaultOptions
, options
);
26 base
.$el
.each(function () {
29 // remove padding on <table> to fix issue #7
30 $
this.css('padding', 0);
32 $
this.wrap('<div class="divTableWithFloatingHeader"></div>');
34 base
.$originalHeader
= $
('thead:first', this);
35 base
.$clonedHeader
= base
.$originalHeader
.clone();
37 base
.$clonedHeader
.addClass('tableFloatingHeader');
38 base
.$clonedHeader
.css({
41 'left': $
this.css('margin-left'),
45 base
.$originalHeader
.addClass('tableFloatingHeaderOriginal');
47 base
.$originalHeader
.before(base
.$clonedHeader
);
49 // enabling support for jquery.tablesorter plugin
50 // forward clicks on clone to original
51 $
('th', base
.$clonedHeader
).click(function(e
){
52 var index
= $
('th', base
.$clonedHeader
).index(this);
53 $
('th', base
.$originalHeader
).eq(index
).click();
55 $
this.bind('sortEnd', base
.updateCloneFromOriginal
);
58 base
.updateTableHeaders();
59 base
.$window
.scroll(base
.updateTableHeaders
);
60 base
.$window
.resize(base
.updateTableHeaders
);
63 base
.updateTableHeaders
= function () {
64 base
.$el
.each(function () {
67 var fixedHeaderHeight
= isNaN(base
.options
.fixedOffset
) ? base
.options
.fixedOffset
.height() : base
.options
.fixedOffset
;
69 var offset
= $
this.offset();
70 var scrollTop
= base
.$window
.scrollTop() + fixedHeaderHeight
;
71 var scrollLeft
= base
.$window
.scrollLeft();
73 if ((scrollTop
> offset
.top
) && (scrollTop
< offset
.top
+ $
this.height())) {
74 base
.$clonedHeader
.css({
75 'top': fixedHeaderHeight
,
77 'left': offset
.left
- scrollLeft
,
81 base
.updateCloneFromOriginal();
84 base
.$clonedHeader
.css('display', 'none');
89 base
.updateCloneFromOriginal
= function () {
90 // Copy cell widths and classes from original header
91 $
('th', base
.$clonedHeader
).each(function (index
) {
93 var origCell
= $
('th', base
.$originalHeader
).eq(index
);
94 $
this.removeClass().addClass(origCell
.attr('class'));
95 $
this.css('width', origCell
.width());
98 // Copy row width from whole table
99 base
.$clonedHeader
.css('width', base
.$originalHeader
.width());
106 $
.StickyTableHeaders
.defaultOptions
= {
110 $
.fn
.stickyTableHeaders
= function (options
) {
111 return this.each(function () {
112 (new $
.StickyTableHeaders(this, options
));