From 384bd78c4c2ca9dc908dd895bdadd789d46f04c5 Mon Sep 17 00:00:00 2001 From: Vitaliy Filippov Date: Fri, 4 Mar 2016 15:33:44 +0300 Subject: [PATCH] Refresh table header on each re-init ("setup") --- jquery.fixedheadertable.js | 13 ++++++++----- jquery.fixedheadertable.min.js | 13 ++++++------- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/jquery.fixedheadertable.js b/jquery.fixedheadertable.js index a89055f..d9ba9ed 100644 --- a/jquery.fixedheadertable.js +++ b/jquery.fixedheadertable.js @@ -2,6 +2,7 @@ * jquery.fixedHeaderTable. The jQuery fixedHeaderTable plugin * * Copyright (c) 2013 Mark Malek + * (c) 2015-2016 Vitaliy Filippov * http://fixedheadertable.com * * Licensed under MIT @@ -10,10 +11,8 @@ * http://docs.jquery.com/Plugins/Authoring * jQuery authoring guidelines * - * Launch : October 2009 - * Version : 1.3 - * Released: May 9th, 2011 - * + * Version: 2016-03-04 + * Based on: Original 1.3 (2011-05-09) * * all CSS sizing (width,height) is done in pixels (px) */ @@ -140,6 +139,10 @@ $thead.clone().appendTo($divHead.find('table')); } else { $divHead = $wrapper.find('div.fht-thead'); + + // Refresh header (it may have changed in between) + $divHead.find('thead').remove(); + $thead.clone().appendTo($divHead.find('table')); } $divHead.find('table.fht-table') .css({ width: widthWithScrollbar }); @@ -178,7 +181,7 @@ }); if (settings.autoResize && !$self.hasClass('fht-table-init')) { - $(window).resize(function() { + $(window).on('resize', function() { $wrapper.find('.fht-cell').remove(); $self.fixedHeaderTable(); methods.resize.apply(self); diff --git a/jquery.fixedheadertable.min.js b/jquery.fixedheadertable.min.js index 6c8a9ef..a6be366 100644 --- a/jquery.fixedheadertable.min.js +++ b/jquery.fixedheadertable.min.js @@ -1,7 +1,8 @@ -/*! +/* * jquery.fixedHeaderTable. The jQuery fixedHeaderTable plugin * * Copyright (c) 2013 Mark Malek + * (c) 2015-2016 Vitaliy Filippov * http://fixedheadertable.com * * Licensed under MIT @@ -10,11 +11,9 @@ * http://docs.jquery.com/Plugins/Authoring * jQuery authoring guidelines * - * Launch : October 2009 - * Version : 1.3 - * Released: May 9th, 2011 - * + * Version: 2016-03-04 + * Based on: Original 1.3 (2011-05-09) * * all CSS sizing (width,height) is done in pixels (px) - */(function(c){c.fn.fixedHeaderTable=function(m){var u={width:"100%",height:"100%",themeClass:"fht-default",borderCollapse:!0,fixedColumns:0,fixedColumn:!1,sortable:!1,autoShow:!0,footer:!1,cloneHeadToFoot:!1,autoResize:!1,create:null},b={},n={init:function(a){b=c.extend({},u,a);return this.each(function(){var a=c(this);h._isTable(a)?(n.setup.apply(this,Array.prototype.slice.call(arguments,1)),c.isFunction(b.create)&&b.create.call(this)):c.error("Invalid table mark-up")})},setup:function(){var a=c(this), d=a.find("thead"),e=a.find("tfoot"),g=0,f,k,p;b.originalTable=c(this).clone();b.includePadding=h._isPaddingIncludedWithWidth();b.scrollbarOffset=h._getScrollbarWidth();b.themeClassName=b.themeClass;f=-1'));f=a.closest(".fht-table-wrapper");!0==b.fixedColumn&&0>=b.fixedColumns&&(b.fixedColumns= 1);0'),c('
').prependTo(f),k=f.find(".fht-fixed-body"));f.css({width:b.width,height:b.height}).addClass(b.themeClassName);a.hasClass("fht-table-init")||a.wrap('
');p=a.closest(".fht-tbody");var l=h._getTableProps(a);h._setupClone(p,l.tbody);a.hasClass("fht-table-init")?k=f.find("div.fht-thead"):(k=0
').prependTo(k): c('
').prependTo(f),k.find("table.fht-table").addClass(b.originalTable.attr("class")).attr("style",b.originalTable.attr("style")),d.clone().appendTo(k.find("table")));h._setupClone(k,l.thead);a.css({"margin-top":-k.outerHeight(!0)});!0==b.footer&&(h._setupTableFooter(a,this,l),e.length||(e=f.find("div.fht-tfoot table")),g=e.outerHeight(!0));d=f.height()-d.outerHeight(!0)-g-l.border;p.css({height:d});a.addClass("fht-table-init");"undefined"!== typeof b.altClass&&n.altRows.apply(this);0
'),p=c('
');a=c('
'); var g=g.width(),l=f.find(".fht-tbody").height()-b.scrollbarOffset,q,t,r,s;k.find("table.fht-table").addClass(b.originalTable.attr("class"));p.find("table.fht-table").addClass(b.originalTable.attr("class"));a.find("table.fht-table").addClass(b.originalTable.attr("class"));q=f.find(".fht-thead thead tr > *:lt("+b.fixedColumns+")");r=b.fixedColumns*e.border;q.each(function(){r+=c(this).outerWidth(!0)});h._fixHeightWithCss(q,e);h._fixWidthWithCss(q,e);var m=[];q.each(function(){m.push(c(this).width())}); t=f.find("tbody tr > *:not(:nth-child(n+"+(b.fixedColumns+1)+"))").each(function(a){h._fixHeightWithCss(c(this),e);h._fixWidthWithCss(c(this),e,m[a%b.fixedColumns])});k.appendTo(d).find("tr").append(q.clone());p.appendTo(d).css({"margin-top":-1,height:l+e.border});t.each(function(a){0==a%b.fixedColumns&&(s=c("").appendTo(p.find("tbody")),b.altClass&&c(this).parent().hasClass(b.altClass)&&s.addClass(b.altClass));c(this).clone().appendTo(s)});d.css({height:0,width:r});var n=d.find(".fht-tbody .fht-table").height()- d.find(".fht-tbody").height();d.find(".fht-tbody .fht-table").bind("mousewheel",function(a,d,b,e){if(0!=e)return a=parseInt(c(this).css("marginTop"),10)+(0 *:lt("+b.fixedColumns+")"),h._fixHeightWithCss(k,e),a.appendTo(d).find("tr").append(k.clone()),d=a.find("table").innerWidth(),a.css({top:b.scrollbarOffset, width:d})},_setupTableFooter:function(a,d,e){d=a.closest(".fht-table-wrapper");var g=a.find("tfoot");a=d.find("div.fht-tfoot");a.length||(a=0
').appendTo(d.find(".fht-fixed-body")):c('
').appendTo(d));a.find("table.fht-table").addClass(b.originalTable.attr("class"));switch(!0){case !g.length&&!0==b.cloneHeadToFoot&&!0==b.footer:e=d.find("div.fht-thead");a.empty(); e.find("table").clone().appendTo(a);break;case g.length&&!1==b.cloneHeadToFoot&&!0==b.footer:a.find("table").append(g).css({"margin-top":-e.border}),h._setupClone(a,e.tfoot)}},_getTableProps:function(a){var d={thead:{},tbody:{},tfoot:{},border:0},c=1;!0==b.borderCollapse&&(c=2);d.border=(a.find("th:first-child").outerWidth()-a.find("th:first-child").innerWidth())/c;d.thead=h._getColumnsWidth(a.find("thead tr"));d.tfoot=h._getColumnsWidth(a.find("tfoot tr"));d.tbody=h._getColumnsWidth(a.find("tbody tr")); return d},_getColumnsWidth:function(a){var d={},b={},g=0,f,k;f=h._getColumnsCount(a);for(k=0;k').appendTo(c(this))).css({width:parseInt(d[a].width,10)}),c(this).closest(".fht-tbody").length||!c(this).is(":last-child")||c(this).closest(".fht-fixed-column").length|| (a=Math.max((c(this).innerWidth()-c(this).width())/2,b.scrollbarOffset),c(this).css({"padding-right":parseInt(c(this).css("padding-right"))+a+"px"})))})})},_isPaddingIncludedWithWidth:function(){var a=c('
test
'),d,e;a.addClass(b.originalTable.attr("class"));a.appendTo("body");d=a.find("td").height();a.find("td").css("height",a.find("tr").height());e=a.find("td").height();a.remove();return d!=e?!0:!1},_getScrollbarWidth:function(){var a= 0;if(!a)if(/msie/.test(navigator.userAgent.toLowerCase())){var b=c('').css({position:"absolute",top:-1E3,left:-1E3}).appendTo("body"),e=c('').css({position:"absolute",top:-1E3,left:-1E3}).appendTo("body"),a=b.width()-e.width()+2;b.add(e).remove()}else b=c("
").css({width:100,height:100,overflow:"auto",position:"absolute",top:-1E3,left:-1E3}).prependTo("body").append("
").find("div").css({width:"100%", height:200}),a=100-b.width(),b.parent().remove();return a}};if(n[m])return n[m].apply(this,Array.prototype.slice.call(arguments,1));if("object"!==typeof m&&m)c.error('Method "'+m+'" does not exist in fixedHeaderTable plugin!');else return n.init.apply(this,arguments)}})(jQuery); - + */ +(function(a){a.fn.fixedHeaderTable=function(f){var e={width:"100%",height:"100%",themeClass:"fht-default",borderCollapse:true,fixedColumns:0,fixedColumn:false,sortable:false,autoShow:true,footer:false,cloneHeadToFoot:false,autoResize:true,create:null};var c={};var b={init:function(g){c=a.extend({},e,g);return this.each(function(){var h=a(this);if(d._isTable(h)){b.setup.apply(this,Array.prototype.slice.call(arguments,1));a.isFunction(c.create)&&c.create.call(this)}else{a.error("Invalid table mark-up")}})},setup:function(){var r=a(this),s=this,m=r.find("thead"),p=r.find("tfoot"),n=0,k,o,j,i,g,q;c.originalTable=a(this).clone();c.includePadding=d._isPaddingIncludedWithWidth();c.scrollbarOffset=d._getScrollbarWidth();c.themeClassName=c.themeClass;if(c.width.search("%")>-1){g=r.parent().width()}else{g=c.width}q=g-c.scrollbarOffset;r.css({width:q});if(!r.closest(".fht-table-wrapper").length){r.addClass("fht-table");r.wrap('
')}k=r.closest(".fht-table-wrapper");if(c.fixedColumn==true&&c.fixedColumns<=0){c.fixedColumns=1}if(c.fixedColumns>0&&k.find(".fht-fixed-column").length==0){r.wrap('
');a('
').prependTo(k);i=k.find(".fht-fixed-body")}k.css({width:c.width,height:c.height}).addClass(c.themeClassName);if(!r.hasClass("fht-table-init")){r.wrap('
')}j=r.closest(".fht-tbody");var h=d._getTableProps(r);d._setupClone(j,h.thead);if(!r.hasClass("fht-table-init")){if(c.fixedColumns>0){o=a('
').prependTo(i)}else{o=a('
').prependTo(k)}o.find("table.fht-table").addClass(c.originalTable.attr("class")).attr("style",c.originalTable.attr("style"));m.clone().appendTo(o.find("table"))}else{o=k.find("div.fht-thead");o.find("thead").remove();m.clone().appendTo(o.find("table"))}o.find("table.fht-table").css({width:g});if(c.footer==true){d._setupTableFooter(r,s,h);if(!p.length){p=k.find("div.fht-tfoot table")}n=p.outerHeight(true)}var l=k.height()-m.outerHeight(true)-n-h.border;if(r.height()-o.outerHeight(true)<=l){o.find("table.fht-table").css({width:q});c.scrollbarOffset=0}d._setupClone(o,h.thead);r.css({"margin-top":-o.outerHeight(true)});j.css({height:l});if(c.autoResize&&!r.hasClass("fht-table-init")){a(window).on("resize",function(){k.find(".fht-cell").remove();r.fixedHeaderTable();b.resize.apply(s)})}r.addClass("fht-table-init");if(typeof(c.altClass)!=="undefined"){b.altRows.apply(s)}if(c.fixedColumns>0){d._setupFixedColumn(r,s,h)}if(!c.autoShow){k.hide()}d._bindScroll(j,h);return s},resize:function(){var g=a(this);g.closest(".fht-table-wrapper").find(".fht-cell").remove();g.fixedHeaderTable();return self},altRows:function(h){var i=a(this),g=(typeof(h)!=="undefined")?h:c.altClass;i.closest(".fht-table-wrapper").find("tbody tr:odd:not(:hidden)").addClass(g)},show:function(j,i,g){var l=a(this),h=this,k=l.closest(".fht-table-wrapper");if(typeof(j)!=="undefined"&&typeof(j)==="number"){k.show(j,function(){a.isFunction(i)&&i.call(this)});return h}else{if(typeof(j)!=="undefined"&&typeof(j)==="string"&&typeof(i)!=="undefined"&&typeof(i)==="number"){k.show(j,i,function(){a.isFunction(g)&&g.call(this)});return h}}l.closest(".fht-table-wrapper").show();a.isFunction(j)&&j.call(this);return h},hide:function(j,i,g){var l=a(this),h=this,k=l.closest(".fht-table-wrapper");if(typeof(j)!=="undefined"&&typeof(j)==="number"){k.hide(j,function(){a.isFunction(g)&&g.call(this)});return h}else{if(typeof(j)!=="undefined"&&typeof(j)==="string"&&typeof(i)!=="undefined"&&typeof(i)==="number"){k.hide(j,i,function(){a.isFunction(g)&&g.call(this)});return h}}l.closest(".fht-table-wrapper").hide();a.isFunction(g)&&g.call(this);return h},destroy:function(){var i=a(this),g=this,h=i.closest(".fht-table-wrapper");i.insertBefore(h).removeAttr("style").append(h.find("tfoot")).removeClass("fht-table fht-table-init").find(".fht-cell").remove();h.remove();return g}};var d={_isTable:function(k){var j=k,h=j.is("table"),i=j.find("thead").length>0,g=j.find("tbody").length>0;if(h&&i&&g){return true}return false},_bindScroll:function(k){var j=k,i=j.closest(".fht-table-wrapper"),h=j.siblings(".fht-thead"),g=j.siblings(".fht-tfoot");j.bind("scroll",function(){if(c.fixedColumns>0){var l=i.find(".fht-fixed-column");l.find(".fht-tbody table").css({"margin-top":-j.scrollTop()})}h.find("table").css({"margin-left":-this.scrollLeft});if(c.footer||c.cloneHeadToFoot){g.find("table").css({"margin-left":-this.scrollLeft})}})},_fixHeightWithCss:function(h,g){if(c.includePadding){h.css({height:h.height()+g.border})}else{h.css({height:h.parent().height()+g.border})}},_fixWidthWithCss:function(i,g,h){if(c.includePadding){i.each(function(){a(this).css({width:h==undefined?a(this).width()+g.border:h+g.border})})}else{i.each(function(){a(this).css({width:h==undefined?a(this).parent().width()+g.border:h+g.border})})}},_setupFixedColumn:function(z,q,k){var y=z,x=y.closest(".fht-table-wrapper"),v=x.find(".fht-fixed-body"),o=x.find(".fht-fixed-column"),p=a('
'),s=a('
'),g=a('
'),i=x.width(),t=v.find(".fht-tbody").height()-c.scrollbarOffset,h,u,r,j,l;p.find("table.fht-table").addClass(c.originalTable.attr("class"));s.find("table.fht-table").addClass(c.originalTable.attr("class"));g.find("table.fht-table").addClass(c.originalTable.attr("class"));h=v.find(".fht-thead thead tr > *:lt("+c.fixedColumns+")");r=c.fixedColumns*k.border;h.each(function(){r+=a(this).outerWidth(true)});d._fixHeightWithCss(h,k);d._fixWidthWithCss(h,k);var n=[];h.each(function(){n.push(a(this).width())});l="tbody tr > *:not(:nth-child(n+"+(c.fixedColumns+1)+"))";u=v.find(l).each(function(B){d._fixHeightWithCss(a(this),k);d._fixWidthWithCss(a(this),k,n[B%c.fixedColumns])});p.appendTo(o).find("tr").append(h.clone());s.appendTo(o).css({"margin-top":-1,height:t+k.border});u.each(function(B){if(B%c.fixedColumns==0){j=a("").appendTo(s.find("tbody"));if(c.altClass&&a(this).parent().hasClass(c.altClass)){j.addClass(c.altClass)}}a(this).clone().appendTo(j)});o.css({height:0,width:r});var A=o.find(".fht-tbody .fht-table").height()-o.find(".fht-tbody").height();o.find(".fht-tbody .fht-table").bind("mousewheel",function(D,F,C,B){if(B==0){return}var E=parseInt(a(this).css("marginTop"),10)+(B>0?120:-120);if(E>0){E=0}if(E<-A){E=-A}a(this).css("marginTop",E);v.find(".fht-tbody").scrollTop(-E).scroll();return false});v.css({width:i});if(c.footer==true||c.cloneHeadToFoot==true){var m=v.find(".fht-tfoot tr > *:lt("+c.fixedColumns+")"),w;d._fixHeightWithCss(m,k);g.appendTo(o).find("tr").append(m.clone());w=g.find("table").innerWidth();g.css({top:c.scrollbarOffset,width:w})}},_setupTableFooter:function(n,l,g){var m=n,k=m.closest(".fht-table-wrapper"),h=m.find("tfoot"),j=k.find("div.fht-tfoot");if(!j.length){if(c.fixedColumns>0){j=a('
').appendTo(k.find(".fht-fixed-body"))}else{j=a('
').appendTo(k)}}j.find("table.fht-table").addClass(c.originalTable.attr("class"));switch(true){case !h.length&&c.cloneHeadToFoot==true&&c.footer==true:var i=k.find("div.fht-thead");j.empty();i.find("table").clone().appendTo(j);break;case h.length&&c.cloneHeadToFoot==false&&c.footer==true:j.find("table").append(h).css({"margin-top":-g.border});d._setupClone(j,g.tfoot);break}},_getTableProps:function(i){var h={thead:{},tbody:{},tfoot:{},border:0},g=1;if(c.borderCollapse==true){g=2}h.border=(i.find("th:first-child").outerWidth()-i.find("th:first-child").innerWidth())/g;i.find("thead tr:first-child > *").each(function(j){h.thead[j]=a(this).width()+h.border});i.find("tfoot tr:first-child > *").each(function(j){h.tfoot[j]=a(this).width()+h.border});i.find("tbody tr:first-child > *").each(function(j){h.tbody[j]=a(this).width()+h.border});return h},_setupClone:function(k,j){var i=k,g=(i.find("thead").length)?"thead tr:first-child > *":(i.find("tfoot").length)?"tfoot tr:first-child > *":"tbody tr:first-child > *",h;i.find(g).each(function(l){h=(a(this).find("div.fht-cell").length)?a(this).find("div.fht-cell"):a('
').appendTo(a(this));h.css({width:parseInt(j[l],10)});if(!a(this).closest(".fht-tbody").length&&a(this).is(":last-child")&&!a(this).closest(".fht-fixed-column").length){a(this).css({"padding-right":parseInt(a(this).css("padding-right"))+c.scrollbarOffset+"px"})}})},_isPaddingIncludedWithWidth:function(){var i=a('
test
'),h,g;i.addClass(c.originalTable.attr("class"));i.appendTo("body");h=i.find("td").height();i.find("td").css("height",i.find("tr").height());g=i.find("td").height();i.remove();if(h!=g){return true}else{return false}},_getScrollbarWidth:function(){var h=0;if(!h){if(/msie/.test(navigator.userAgent.toLowerCase())){var j=a('').css({position:"absolute",top:-1000,left:-1000}).appendTo("body"),i=a('').css({position:"absolute",top:-1000,left:-1000}).appendTo("body");h=j.width()-i.width()+2;j.add(i).remove()}else{var g=a("
").css({width:100,height:100,overflow:"auto",position:"absolute",top:-1000,left:-1000}).prependTo("body").append("
").find("div").css({width:"100%",height:200});h=100-g.width();g.parent().remove()}}return h}};if(b[f]){return b[f].apply(this,Array.prototype.slice.call(arguments,1))}else{if(typeof f==="object"||!f){return b.init.apply(this,arguments)}else{a.error('Method "'+f+'" does not exist in fixedHeaderTable plugin!')}}}})(jQuery); \ No newline at end of file