diff --git a/js/jquery.stickytableheaders.js b/js/jquery.stickytableheaders.js index b225057..9cb2f8f 100644 --- a/js/jquery.stickytableheaders.js +++ b/js/jquery.stickytableheaders.js @@ -9,7 +9,9 @@ defaults = { fixedOffset: 0, leftOffset: 0, - scrollableArea: window + scrollableArea: window, + headSelector: 'thead:first', + cellSelector: 'th,td' }; function Plugin (el, options) { @@ -47,7 +49,7 @@ base.$scrollableArea = $(base.options.scrollableArea); - base.$originalHeader = $('thead:first', this); + base.$originalHeader = $(base.options.headSelector, this); base.$clonedHeader = base.$originalHeader.clone(); $this.trigger('clonedHeader.' + name, [base.$clonedHeader]); @@ -156,7 +158,7 @@ base.$originalHeader.css('position', 'static'); base.$clonedHeader.css('display', 'none'); base.isSticky = false; - base.resetWidth($("td,th", base.$clonedHeader), $("td,th", base.$originalHeader)); + base.resetWidth($(base.options.cellSelector, base.$clonedHeader), $(base.options.cellSelector, base.$originalHeader)); } }); @@ -182,8 +184,8 @@ return; } // Copy cell widths from clone - var $origHeaders = $('th,td', base.$originalHeader); - var $clonedHeaders = $('th,td', base.$clonedHeader); + var $origHeaders = $(base.options.cellSelector, base.$originalHeader); + var $clonedHeaders = $(base.options.cellSelector, base.$clonedHeader); base.cellWidths = []; base.getWidth($clonedHeaders); base.setWidth($clonedHeaders, $origHeaders); diff --git a/js/jquery.stickytableheaders.min.js b/js/jquery.stickytableheaders.min.js index ff0786e..226405d 100644 --- a/js/jquery.stickytableheaders.min.js +++ b/js/jquery.stickytableheaders.min.js @@ -1 +1 @@ -(function(e,t){"use strict";function i(i,s){var n=this;n.$el=e(i),n.el=i,n.id=a++,n.$el.bind("destroyed",e.proxy(n.teardown,n)),n.$clonedHeader=null,n.$originalHeader=null,n.isSticky=!1,n.hasBeenSticky=!1,n.leftOffset=null,n.topOffset=null,n.init=function(){n.options=e.extend({},o,s),n.$el.each(function(){var t=e(this);t.css("padding",0),n.$scrollableArea=e(n.options.scrollableArea),n.$originalHeader=e("thead:first",this),n.$clonedHeader=n.$originalHeader.clone(),t.trigger("clonedHeader."+l,[n.$clonedHeader]),n.$clonedHeader.addClass("tableFloatingHeader"),n.$clonedHeader.css("display","none"),n.$originalHeader.addClass("tableFloatingHeaderOriginal"),n.$originalHeader.after(n.$clonedHeader),n.$printStyle=e(''),e("head").append(n.$printStyle)}),n.updateWidth(),n.toggleHeaders(),n.bind()},n.destroy=function(){n.$el.unbind("destroyed",n.teardown),n.teardown()},n.teardown=function(){n.isSticky&&n.$originalHeader.css("position","static"),e.removeData(n.el,"plugin_"+l),n.unbind(),n.$clonedHeader.remove(),n.$originalHeader.removeClass("tableFloatingHeaderOriginal"),n.$originalHeader.css("visibility","visible"),n.$printStyle.remove(),n.el=null,n.$el=null},n.bind=function(){n.$scrollableArea.on("scroll."+l,n.toggleHeaders),n.$scrollableArea[0]!==t&&(e(t).on("scroll."+l+n.id,n.setPositionValues),e(t).on("resize."+l+n.id,n.toggleHeaders)),n.$scrollableArea.on("resize."+l,n.toggleHeaders),n.$scrollableArea.on("resize."+l,n.updateWidth)},n.unbind=function(){n.$scrollableArea.off("."+l,n.toggleHeaders),n.$scrollableArea[0]!==t&&(e(t).off("."+l+n.id,n.setPositionValues),e(t).off("."+l+n.id,n.toggleHeaders)),n.$scrollableArea.off("."+l,n.updateWidth),n.$el.off("."+l),n.$el.find("*").off("."+l)},n.toggleHeaders=function(){n.$el&&n.$el.each(function(){var i,l=e(this),a=n.$scrollableArea[0]===t?isNaN(n.options.fixedOffset)?n.options.fixedOffset.height():n.options.fixedOffset:n.$scrollableArea.offset().top+(isNaN(n.options.fixedOffset)?0:n.options.fixedOffset),o=l.offset(),s=n.$scrollableArea.scrollTop()+a,r=n.$scrollableArea.scrollLeft(),d=n.$scrollableArea[0]===t?s>o.top:a>o.top,c=(n.$scrollableArea[0]===t?s:0)i||i+e(t).height()>e(document).height()||0>l||l+e(t).width()>e(document).width()||n.$originalHeader.css({top:n.topOffset-(n.$scrollableArea[0]===t?0:i),left:n.leftOffset-(n.$scrollableArea[0]===t?0:l)})},n.updateWidth=function(){if(n.isSticky){var t=e("th,td",n.$originalHeader),i=e("th,td",n.$clonedHeader);n.cellWidths=[],n.getWidth(i),n.setWidth(i,t),n.$originalHeader.css("width",n.$clonedHeader.width())}},n.getWidth=function(t){t.each(function(t){var i,l=e(this);i="border-box"===l.css("box-sizing")?l.outerWidth():l.width(),n.cellWidths[t]=i})},n.setWidth=function(e,t){e.each(function(e){var i=n.cellWidths[e];t.eq(e).css({"min-width":i,"max-width":i})})},n.resetWidth=function(t,i){t.each(function(t){var l=e(this);i.eq(t).css({"min-width":l.css("min-width"),"max-width":l.css("max-width")})})},n.updateOptions=function(t){n.options=e.extend({},o,t),n.updateWidth(),n.toggleHeaders()},n.init()}var l="stickyTableHeaders",a=0,o={fixedOffset:0,leftOffset:0,scrollableArea:t};e.fn[l]=function(t){return this.each(function(){var a=e.data(this,"plugin_"+l);a?"string"==typeof t?a[t].apply(a):a.updateOptions(t):"destroy"!==t&&e.data(this,"plugin_"+l,new i(this,t))})}})(jQuery,window); \ No newline at end of file +!function(a,b){"use strict";function c(c,g){var h=this;h.$el=a(c),h.el=c,h.id=e++,h.$el.bind("destroyed",a.proxy(h.teardown,h)),h.$clonedHeader=null,h.$originalHeader=null,h.isSticky=!1,h.hasBeenSticky=!1,h.leftOffset=null,h.topOffset=null,h.init=function(){h.options=a.extend({},f,g),h.$el.each(function(){var b=a(this);b.css("padding",0),h.$scrollableArea=a(h.options.scrollableArea),h.$originalHeader=a(h.options.headSelector,this),h.$clonedHeader=h.$originalHeader.clone(),b.trigger("clonedHeader."+d,[h.$clonedHeader]),h.$clonedHeader.addClass("tableFloatingHeader"),h.$clonedHeader.css("display","none"),h.$originalHeader.addClass("tableFloatingHeaderOriginal"),h.$originalHeader.after(h.$clonedHeader),h.$printStyle=a(''),a("head").append(h.$printStyle)}),h.updateWidth(),h.toggleHeaders(),h.bind()},h.destroy=function(){h.$el.unbind("destroyed",h.teardown),h.teardown()},h.teardown=function(){h.isSticky&&h.$originalHeader.css("position","static"),a.removeData(h.el,"plugin_"+d),h.unbind(),h.$clonedHeader.remove(),h.$originalHeader.removeClass("tableFloatingHeaderOriginal"),h.$originalHeader.css("visibility","visible"),h.$printStyle.remove(),h.el=null,h.$el=null},h.bind=function(){h.$scrollableArea.on("scroll."+d,h.toggleHeaders),h.$scrollableArea[0]!==b&&(a(b).on("scroll."+d+h.id,h.setPositionValues),a(b).on("resize."+d+h.id,h.toggleHeaders)),h.$scrollableArea.on("resize."+d,h.toggleHeaders),h.$scrollableArea.on("resize."+d,h.updateWidth)},h.unbind=function(){h.$scrollableArea.off("."+d,h.toggleHeaders),h.$scrollableArea[0]!==b&&(a(b).off("."+d+h.id,h.setPositionValues),a(b).off("."+d+h.id,h.toggleHeaders)),h.$scrollableArea.off("."+d,h.updateWidth),h.$el.off("."+d),h.$el.find("*").off("."+d)},h.toggleHeaders=function(){h.$el&&h.$el.each(function(){var c,d=a(this),e=h.$scrollableArea[0]===b?isNaN(h.options.fixedOffset)?h.options.fixedOffset.height():h.options.fixedOffset:h.$scrollableArea.offset().top+(isNaN(h.options.fixedOffset)?0:h.options.fixedOffset),f=d.offset(),g=h.$scrollableArea.scrollTop()+e,i=h.$scrollableArea.scrollLeft(),j=h.$scrollableArea[0]===b?g>f.top:e>f.top,k=(h.$scrollableArea[0]===b?g:0)c||c+a(b).height()>a(document).height()||0>d||d+a(b).width()>a(document).width()||h.$originalHeader.css({top:h.topOffset-(h.$scrollableArea[0]===b?0:c),left:h.leftOffset-(h.$scrollableArea[0]===b?0:d)})},h.updateWidth=function(){if(h.isSticky){var b=a(h.options.cellSelector,h.$originalHeader),c=a(h.options.cellSelector,h.$clonedHeader);h.cellWidths=[],h.getWidth(c),h.setWidth(c,b),h.$originalHeader.css("width",h.$clonedHeader.width())}},h.getWidth=function(b){b.each(function(b){var c,d=a(this);c="border-box"===d.css("box-sizing")?d.outerWidth():d.width(),h.cellWidths[b]=c})},h.setWidth=function(a,b){a.each(function(a){var c=h.cellWidths[a];b.eq(a).css({"min-width":c,"max-width":c})})},h.resetWidth=function(b,c){b.each(function(b){var d=a(this);c.eq(b).css({"min-width":d.css("min-width"),"max-width":d.css("max-width")})})},h.updateOptions=function(b){h.options=a.extend({},f,b),h.updateWidth(),h.toggleHeaders()},h.init()}var d="stickyTableHeaders",e=0,f={fixedOffset:0,leftOffset:0,scrollableArea:b,headSelector:"thead:first",cellSelector:"th,td"};a.fn[d]=function(b){return this.each(function(){var e=a.data(this,"plugin_"+d);e?"string"==typeof b?e[b].apply(e):e.updateOptions(b):"destroy"!==b&&a.data(this,"plugin_"+d,new c(this,b))})}}(jQuery,window); \ No newline at end of file