diff --git a/jquery.fixedheadertable.js b/jquery.fixedheadertable.js index 1d052d8..ef1791d 100644 --- a/jquery.fixedheadertable.js +++ b/jquery.fixedheadertable.js @@ -39,6 +39,9 @@ }; var settings = {}; + var body = undefined; + var header = undefined; + var footer = undefined; // public methods var methods = { @@ -332,23 +335,30 @@ $self.bind('scroll', function() { if (settings.fixedColumns > 0) { var $fixedColumns = $wrapper.find('.fht-fixed-column'); + var margin = -$self.scrollTop(); + + if (body === undefined) { + body = $fixedColumns.find('.fht-tbody table'); + } + body.css({ + 'margin-top': margin + }); + } - $fixedColumns.find('.fht-tbody table') - .css({ - 'margin-top': -$self.scrollTop() - }); + if (header === undefined) { + header = $thead.find('table'); } + header.css({ + 'margin-left': -this.scrollLeft + }); - $thead.find('table') - .css({ + if (settings.footer || settings.cloneHeadToFoot) { + if (footer === undefined) { + footer = $tfoot.find('table'); + } + footer.css({ 'margin-left': -this.scrollLeft }); - - if (settings.footer || settings.cloneHeadToFoot) { - $tfoot.find('table') - .css({ - 'margin-left': -this.scrollLeft - }); } }); }, @@ -703,4 +713,4 @@ }; -})(jQuery); +})(jQuery); \ No newline at end of file diff --git a/jquery.fixedheadertable.min.js b/jquery.fixedheadertable.min.js index 6c8a9ef..8119a89 100644 --- a/jquery.fixedheadertable.min.js +++ b/jquery.fixedheadertable.min.js @@ -16,5 +16,6 @@ * * * 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(e){e.fn.fixedHeaderTable=function(t){var n={width:"100%",height:"100%",themeClass:"fht-default",borderCollapse:true,fixedColumns:0,fixedColumn:false,sortable:false,autoShow:true,footer:false,cloneHeadToFoot:false,autoResize:false,create:null};var r={};var i=undefined;var s=undefined;var o=undefined;var u={init:function(t){r=e.extend({},n,t);return this.each(function(){var t=e(this);if(a._isTable(t)){u.setup.apply(this,Array.prototype.slice.call(arguments,1));e.isFunction(r.create)&&r.create.call(this)}else{e.error("Invalid table mark-up")}})},setup:function(){var t=e(this),n=this,i=t.find("thead"),s=t.find("tfoot"),o=0,f,l,c,h,p;r.originalTable=e(this).clone();r.includePadding=a._isPaddingIncludedWithWidth();r.scrollbarOffset=a._getScrollbarWidth();r.themeClassName=r.themeClass;if(r.width.search("%")>-1){p=t.parent().width()-r.scrollbarOffset}else{p=r.width-r.scrollbarOffset}t.css({width:p});if(!t.closest(".fht-table-wrapper").length){t.addClass("fht-table");t.wrap('
')}f=t.closest(".fht-table-wrapper");if(r.fixedColumn==true&&r.fixedColumns<=0){r.fixedColumns=1}if(r.fixedColumns>0&&f.find(".fht-fixed-column").length==0){t.wrap('
');e('
').prependTo(f);h=f.find(".fht-fixed-body")}f.css({width:r.width,height:r.height}).addClass(r.themeClassName);if(!t.hasClass("fht-table-init")){t.wrap('
')}c=t.closest(".fht-tbody");var d=a._getTableProps(t);a._setupClone(c,d.tbody);if(!t.hasClass("fht-table-init")){if(r.fixedColumns>0){l=e('
').prependTo(h)}else{l=e('
').prependTo(f)}l.find("table.fht-table").addClass(r.originalTable.attr("class")).attr("style",r.originalTable.attr("style"));i.clone().appendTo(l.find("table"))}else{l=f.find("div.fht-thead")}a._setupClone(l,d.thead);t.css({"margin-top":-l.outerHeight(true)});if(r.footer==true){a._setupTableFooter(t,n,d);if(!s.length){s=f.find("div.fht-tfoot table")}o=s.outerHeight(true)}var v=f.height()-i.outerHeight(true)-o-d.border;c.css({height:v});t.addClass("fht-table-init");if(typeof r.altClass!=="undefined"){u.altRows.apply(n)}if(r.fixedColumns>0){a._setupFixedColumn(t,n,d)}if(!r.autoShow){f.hide()}a._bindScroll(c,d);return n},resize:function(){var e=this;return e},altRows:function(t){var n=e(this),i=typeof t!=="undefined"?t:r.altClass;n.closest(".fht-table-wrapper").find("tbody tr:odd:not(:hidden)").addClass(i)},show:function(t,n,r){var i=e(this),s=this,o=i.closest(".fht-table-wrapper");if(typeof t!=="undefined"&&typeof t==="number"){o.show(t,function(){e.isFunction(n)&&n.call(this)});return s}else if(typeof t!=="undefined"&&typeof t==="string"&&typeof n!=="undefined"&&typeof n==="number"){o.show(t,n,function(){e.isFunction(r)&&r.call(this)});return s}i.closest(".fht-table-wrapper").show();e.isFunction(t)&&t.call(this);return s},hide:function(t,n,r){var i=e(this),s=this,o=i.closest(".fht-table-wrapper");if(typeof t!=="undefined"&&typeof t==="number"){o.hide(t,function(){e.isFunction(r)&&r.call(this)});return s}else if(typeof t!=="undefined"&&typeof t==="string"&&typeof n!=="undefined"&&typeof n==="number"){o.hide(t,n,function(){e.isFunction(r)&&r.call(this)});return s}i.closest(".fht-table-wrapper").hide();e.isFunction(r)&&r.call(this);return s},destroy:function(){var t=e(this),n=this,r=t.closest(".fht-table-wrapper");t.insertBefore(r).removeAttr("style").append(r.find("tfoot")).removeClass("fht-table fht-table-init").find(".fht-cell").remove();r.remove();return n}};var a={_isTable:function(e){var t=e,n=t.is("table"),r=t.find("thead").length>0,i=t.find("tbody").length>0;if(n&&r&&i){return true}return false},_bindScroll:function(e){var t=e,n=t.closest(".fht-table-wrapper"),u=t.siblings(".fht-thead"),a=t.siblings(".fht-tfoot");t.bind("scroll",function(){if(r.fixedColumns>0){var e=n.find(".fht-fixed-column");var f=-t.scrollTop();if(i===undefined){i=e.find(".fht-tbody table")}i.css({"margin-top":f})}if(s===undefined){s=u.find("table")}s.css({"margin-left":-this.scrollLeft});if(r.footer||r.cloneHeadToFoot){if(o===undefined){o=a.find("table")}o.css({"margin-left":-this.scrollLeft})}})},_fixHeightWithCss:function(e,t){if(r.includePadding){e.css({height:e.height()+t.border})}else{e.css({height:e.parent().height()+t.border})}},_fixWidthWithCss:function(t,n,i){if(r.includePadding){t.each(function(){e(this).css({width:i==undefined?e(this).width()+n.border:i+n.border})})}else{t.each(function(){e(this).css({width:i==undefined?e(this).parent().width()+n.border:i+n.border})})}},_setupFixedColumn:function(t,n,i){var s=t,o=s.closest(".fht-table-wrapper"),u=o.find(".fht-fixed-body"),f=o.find(".fht-fixed-column"),l=e('
'),c=e('
'),h=e('
'),p=o.width(),d=u.find(".fht-tbody").height()-r.scrollbarOffset,v,m,g,y,b;l.find("table.fht-table").addClass(r.originalTable.attr("class"));c.find("table.fht-table").addClass(r.originalTable.attr("class"));h.find("table.fht-table").addClass(r.originalTable.attr("class"));v=u.find(".fht-thead thead tr > *:lt("+r.fixedColumns+")");g=r.fixedColumns*i.border;v.each(function(){g+=e(this).outerWidth(true)});a._fixHeightWithCss(v,i);a._fixWidthWithCss(v,i);var w=[];v.each(function(){w.push(e(this).width())});b="tbody tr > *:not(:nth-child(n+"+(r.fixedColumns+1)+"))";m=u.find(b).each(function(t){a._fixHeightWithCss(e(this),i);a._fixWidthWithCss(e(this),i,w[t%r.fixedColumns])});l.appendTo(f).find("tr").append(v.clone());c.appendTo(f).css({"margin-top":-1,height:d+i.border});m.each(function(t){if(t%r.fixedColumns==0){y=e("").appendTo(c.find("tbody"));if(r.altClass&&e(this).parent().hasClass(r.altClass)){y.addClass(r.altClass)}}e(this).clone().appendTo(y)});f.css({height:0,width:g});var E=f.find(".fht-tbody .fht-table").height()-f.find(".fht-tbody").height();f.find(".fht-tbody .fht-table").bind("mousewheel",function(t,n,r,i){if(i==0){return}var s=parseInt(e(this).css("marginTop"),10)+(i>0?120:-120);if(s>0){s=0}if(s<-E){s=-E}e(this).css("marginTop",s);u.find(".fht-tbody").scrollTop(-s).scroll();return false});u.css({width:p});if(r.footer==true||r.cloneHeadToFoot==true){var S=u.find(".fht-tfoot tr > *:lt("+r.fixedColumns+")"),x;a._fixHeightWithCss(S,i);h.appendTo(f).find("tr").append(S.clone());x=h.find("table").innerWidth();h.css({top:r.scrollbarOffset,width:x})}},_setupTableFooter:function(t,n,i){var s=t,o=s.closest(".fht-table-wrapper"),u=s.find("tfoot"),f=o.find("div.fht-tfoot");if(!f.length){if(r.fixedColumns>0){f=e('
').appendTo(o.find(".fht-fixed-body"))}else{f=e('
').appendTo(o)}}f.find("table.fht-table").addClass(r.originalTable.attr("class"));switch(true){case!u.length&&r.cloneHeadToFoot==true&&r.footer==true:var l=o.find("div.fht-thead");f.empty();l.find("table").clone().appendTo(f);break;case u.length&&r.cloneHeadToFoot==false&&r.footer==true:f.find("table").append(u).css({"margin-top":-i.border});a._setupClone(f,i.tfoot);break}},_getTableProps:function(t){var n={thead:{},tbody:{},tfoot:{},border:0},i=1;if(r.borderCollapse==true){i=2}n.border=(t.find("th:first-child").outerWidth()-t.find("th:first-child").innerWidth())/i;t.find("thead tr:first-child > *").each(function(t){n.thead[t]=e(this).width()+n.border});t.find("tfoot tr:first-child > *").each(function(t){n.tfoot[t]=e(this).width()+n.border});t.find("tbody tr:first-child > *").each(function(t){n.tbody[t]=e(this).width()+n.border});return n},_setupClone:function(t,n){var i=t,s=i.find("thead").length?"thead tr:first-child > *":i.find("tfoot").length?"tfoot tr:first-child > *":"tbody tr:first-child > *",o;i.find(s).each(function(t){o=e(this).find("div.fht-cell").length?e(this).find("div.fht-cell"):e('
').appendTo(e(this));o.css({width:parseInt(n[t],10)});if(!e(this).closest(".fht-tbody").length&&e(this).is(":last-child")&&!e(this).closest(".fht-fixed-column").length){var i=Math.max((e(this).innerWidth()-e(this).width())/2,r.scrollbarOffset);e(this).css({"padding-right":parseInt(e(this).css("padding-right"))+i+"px"})}})},_isPaddingIncludedWithWidth:function(){var t=e('
test
'),n,i;t.addClass(r.originalTable.attr("class"));t.appendTo("body");n=t.find("td").height();t.find("td").css("height",t.find("tr").height());i=t.find("td").height();t.remove();if(n!=i){return true}else{return false}},_getScrollbarWidth:function(){var t=0;if(!t){if(/msie/.test(navigator.userAgent.toLowerCase())){var n=e('').css({position:"absolute",top:-1e3,left:-1e3}).appendTo("body"),r=e('').css({position:"absolute",top:-1e3,left:-1e3}).appendTo("body");t=n.width()-r.width()+2;n.add(r).remove()}else{var i=e("
").css({width:100,height:100,overflow:"auto",position:"absolute",top:-1e3,left:-1e3}).prependTo("body").append("
").find("div").css({width:"100%",height:200});t=100-i.width();i.parent().remove()}}return t}};if(u[t]){return u[t].apply(this,Array.prototype.slice.call(arguments,1))}else if(typeof t==="object"||!t){return u.init.apply(this,arguments)}else{e.error('Method "'+t+'" does not exist in fixedHeaderTable plugin!')}}})(jQuery) +