diff --git a/jquery.fixedheadertable.js b/jquery.fixedheadertable.js index 1d052d8..4e15b50 100644 --- a/jquery.fixedheadertable.js +++ b/jquery.fixedheadertable.js @@ -78,10 +78,15 @@ settings.scrollbarOffset = helpers._getScrollbarWidth(); settings.themeClassName = settings.themeClass; + if (settings.width.search('%') > -1) { - widthMinusScrollbar = $self.parent().width() - settings.scrollbarOffset; + widthMinusScrollbar = $self.parent().width(); } else { - widthMinusScrollbar = settings.width - settings.scrollbarOffset; + widthMinusScrollbar = settings.width; + } + + if($self.height() > $self.parent().height()) { + widthMinusScrollbar -= settings.scrollbarOffset; } $self.css({ @@ -426,7 +431,7 @@ tdWidths.push($(this).width()); }); - firstTdChildrenSelector = 'tbody tr > *:not(:nth-child(n+' + (settings.fixedColumns + 1) + '))'; + firstTdChildrenSelector = 'tbody:first > tr > *:not(:nth-child(n+' + (settings.fixedColumns + 1) + '))'; $firstTdChildren = $fixedBody.find(firstTdChildrenSelector) .each(function(index) { helpers._fixHeightWithCss($(this), tableProps); @@ -459,7 +464,6 @@ // set width of fixed column wrapper $fixedColumn.css({ - 'height': 0, 'width': fixedColumnWidth }); diff --git a/jquery.fixedheadertable.min.js b/jquery.fixedheadertable.min.js index 6c8a9ef..a45efd6 100644 --- a/jquery.fixedheadertable.min.js +++ b/jquery.fixedheadertable.min.js @@ -16,5 +16,4 @@ * * * 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($){$.fn.fixedHeaderTable=function(method){var defaults={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 settings={};var methods={init:function(options){settings=$.extend({},defaults,options);return this.each(function(){var $self=$(this);if(helpers._isTable($self)){methods.setup.apply(this,Array.prototype.slice.call(arguments,1));$.isFunction(settings.create)&&settings.create.call(this)}else{$.error("Invalid table mark-up")}})},setup:function(){var $self=$(this),self=this,$thead=$self.find("thead"),$tfoot=$self.find("tfoot"),tfootHeight=0,$wrapper,$divHead,$divBody,$fixedBody,widthMinusScrollbar;settings.originalTable=$(this).clone();settings.includePadding=helpers._isPaddingIncludedWithWidth();settings.scrollbarOffset=helpers._getScrollbarWidth();settings.themeClassName=settings.themeClass;if(settings.width.search("%")>-1){widthMinusScrollbar=$self.parent().width()}else{widthMinusScrollbar=settings.width}if($self.height()>$self.parent().height()){widthMinusScrollbar-=settings.scrollbarOffset}$self.css({width:widthMinusScrollbar});if(!$self.closest(".fht-table-wrapper").length){$self.addClass("fht-table");$self.wrap('
')}$wrapper=$self.closest(".fht-table-wrapper");if(settings.fixedColumn==true&&settings.fixedColumns<=0){settings.fixedColumns=1}if(settings.fixedColumns>0&&$wrapper.find(".fht-fixed-column").length==0){$self.wrap('
');$('
').prependTo($wrapper);$fixedBody=$wrapper.find(".fht-fixed-body")}$wrapper.css({width:settings.width,height:settings.height}).addClass(settings.themeClassName);if(!$self.hasClass("fht-table-init")){$self.wrap('
')}$divBody=$self.closest(".fht-tbody");var tableProps=helpers._getTableProps($self);helpers._setupClone($divBody,tableProps.tbody);if(!$self.hasClass("fht-table-init")){if(settings.fixedColumns>0){$divHead=$('
').prependTo($fixedBody)}else{$divHead=$('
').prependTo($wrapper)}$divHead.find("table.fht-table").addClass(settings.originalTable.attr("class")).attr("style",settings.originalTable.attr("style"));$thead.clone().appendTo($divHead.find("table"))}else{$divHead=$wrapper.find("div.fht-thead")}helpers._setupClone($divHead,tableProps.thead);$self.css({"margin-top":-$divHead.outerHeight(true)});if(settings.footer==true){helpers._setupTableFooter($self,self,tableProps);if(!$tfoot.length){$tfoot=$wrapper.find("div.fht-tfoot table")}tfootHeight=$tfoot.outerHeight(true)}var tbodyHeight=$wrapper.height()-$thead.outerHeight(true)-tfootHeight-tableProps.border;$divBody.css({height:tbodyHeight});$self.addClass("fht-table-init");if(typeof settings.altClass!=="undefined"){methods.altRows.apply(self)}if(settings.fixedColumns>0){helpers._setupFixedColumn($self,self,tableProps)}if(!settings.autoShow){$wrapper.hide()}helpers._bindScroll($divBody,tableProps);return self},resize:function(){var self=this;return self},altRows:function(arg1){var $self=$(this),altClass=typeof arg1!=="undefined"?arg1:settings.altClass;$self.closest(".fht-table-wrapper").find("tbody tr:odd:not(:hidden)").addClass(altClass)},show:function(arg1,arg2,arg3){var $self=$(this),self=this,$wrapper=$self.closest(".fht-table-wrapper");if(typeof arg1!=="undefined"&&typeof arg1==="number"){$wrapper.show(arg1,function(){$.isFunction(arg2)&&arg2.call(this)});return self}else if(typeof arg1!=="undefined"&&typeof arg1==="string"&&typeof arg2!=="undefined"&&typeof arg2==="number"){$wrapper.show(arg1,arg2,function(){$.isFunction(arg3)&&arg3.call(this)});return self}$self.closest(".fht-table-wrapper").show();$.isFunction(arg1)&&arg1.call(this);return self},hide:function(arg1,arg2,arg3){var $self=$(this),self=this,$wrapper=$self.closest(".fht-table-wrapper");if(typeof arg1!=="undefined"&&typeof arg1==="number"){$wrapper.hide(arg1,function(){$.isFunction(arg3)&&arg3.call(this)});return self}else if(typeof arg1!=="undefined"&&typeof arg1==="string"&&typeof arg2!=="undefined"&&typeof arg2==="number"){$wrapper.hide(arg1,arg2,function(){$.isFunction(arg3)&&arg3.call(this)});return self}$self.closest(".fht-table-wrapper").hide();$.isFunction(arg3)&&arg3.call(this);return self},destroy:function(){var $self=$(this),self=this,$wrapper=$self.closest(".fht-table-wrapper");$self.insertBefore($wrapper).removeAttr("style").append($wrapper.find("tfoot")).removeClass("fht-table fht-table-init").find(".fht-cell").remove();$wrapper.remove();return self}};var helpers={_isTable:function($obj){var $self=$obj,hasTable=$self.is("table"),hasThead=$self.find("thead").length>0,hasTbody=$self.find("tbody").length>0;if(hasTable&&hasThead&&hasTbody){return true}return false},_bindScroll:function($obj){var $self=$obj,$wrapper=$self.closest(".fht-table-wrapper"),$thead=$self.siblings(".fht-thead"),$tfoot=$self.siblings(".fht-tfoot");$self.bind("scroll",function(){if(settings.fixedColumns>0){var $fixedColumns=$wrapper.find(".fht-fixed-column");$fixedColumns.find(".fht-tbody table").css({"margin-top":-$self.scrollTop()})}$thead.find("table").css({"margin-left":-this.scrollLeft});if(settings.footer||settings.cloneHeadToFoot){$tfoot.find("table").css({"margin-left":-this.scrollLeft})}})},_fixHeightWithCss:function($obj,tableProps){if(settings.includePadding){$obj.css({height:$obj.height()+tableProps.border})}else{$obj.css({height:$obj.parent().height()+tableProps.border})}},_fixWidthWithCss:function($obj,tableProps,width){if(settings.includePadding){$obj.each(function(){$(this).css({width:width==undefined?$(this).width()+tableProps.border:width+tableProps.border})})}else{$obj.each(function(){$(this).css({width:width==undefined?$(this).parent().width()+tableProps.border:width+tableProps.border})})}},_setupFixedColumn:function($obj,obj,tableProps){var $self=$obj,$wrapper=$self.closest(".fht-table-wrapper"),$fixedBody=$wrapper.find(".fht-fixed-body"),$fixedColumn=$wrapper.find(".fht-fixed-column"),$thead=$('
'),$tbody=$('
'),$tfoot=$('
'),fixedBodyWidth=$wrapper.width(),fixedBodyHeight=$fixedBody.find(".fht-tbody").height()-settings.scrollbarOffset,$firstThChildren,$firstTdChildren,fixedColumnWidth,$newRow,firstTdChildrenSelector;$thead.find("table.fht-table").addClass(settings.originalTable.attr("class"));$tbody.find("table.fht-table").addClass(settings.originalTable.attr("class"));$tfoot.find("table.fht-table").addClass(settings.originalTable.attr("class"));$firstThChildren=$fixedBody.find(".fht-thead thead tr > *:lt("+settings.fixedColumns+")");fixedColumnWidth=settings.fixedColumns*tableProps.border;$firstThChildren.each(function(){fixedColumnWidth+=$(this).outerWidth(true)});helpers._fixHeightWithCss($firstThChildren,tableProps);helpers._fixWidthWithCss($firstThChildren,tableProps);var tdWidths=[];$firstThChildren.each(function(){tdWidths.push($(this).width())});firstTdChildrenSelector="tbody:first > tr > *:not(:nth-child(n+"+(settings.fixedColumns+1)+"))";$firstTdChildren=$fixedBody.find(firstTdChildrenSelector).each(function(index){helpers._fixHeightWithCss($(this),tableProps);helpers._fixWidthWithCss($(this),tableProps,tdWidths[index%settings.fixedColumns])});$thead.appendTo($fixedColumn).find("tr").append($firstThChildren.clone());$tbody.appendTo($fixedColumn).css({"margin-top":-1,height:fixedBodyHeight+tableProps.border});$firstTdChildren.each(function(index){if(index%settings.fixedColumns==0){$newRow=$("").appendTo($tbody.find("tbody"));if(settings.altClass&&$(this).parent().hasClass(settings.altClass)){$newRow.addClass(settings.altClass)}}$(this).clone().appendTo($newRow)});$fixedColumn.css({width:fixedColumnWidth});var maxTop=$fixedColumn.find(".fht-tbody .fht-table").height()-$fixedColumn.find(".fht-tbody").height();$fixedColumn.find(".fht-tbody .fht-table").bind("mousewheel",function(event,delta,deltaX,deltaY){if(deltaY==0){return}var top=parseInt($(this).css("marginTop"),10)+(deltaY>0?120:-120);if(top>0){top=0}if(top<-maxTop){top=-maxTop}$(this).css("marginTop",top);$fixedBody.find(".fht-tbody").scrollTop(-top).scroll();return false});$fixedBody.css({width:fixedBodyWidth});if(settings.footer==true||settings.cloneHeadToFoot==true){var $firstTdFootChild=$fixedBody.find(".fht-tfoot tr > *:lt("+settings.fixedColumns+")"),footwidth;helpers._fixHeightWithCss($firstTdFootChild,tableProps);$tfoot.appendTo($fixedColumn).find("tr").append($firstTdFootChild.clone());footwidth=$tfoot.find("table").innerWidth();$tfoot.css({top:settings.scrollbarOffset,width:footwidth})}},_setupTableFooter:function($obj,obj,tableProps){var $self=$obj,$wrapper=$self.closest(".fht-table-wrapper"),$tfoot=$self.find("tfoot"),$divFoot=$wrapper.find("div.fht-tfoot");if(!$divFoot.length){if(settings.fixedColumns>0){$divFoot=$('
').appendTo($wrapper.find(".fht-fixed-body"))}else{$divFoot=$('
').appendTo($wrapper)}}$divFoot.find("table.fht-table").addClass(settings.originalTable.attr("class"));switch(true){case!$tfoot.length&&settings.cloneHeadToFoot==true&&settings.footer==true:var $divHead=$wrapper.find("div.fht-thead");$divFoot.empty();$divHead.find("table").clone().appendTo($divFoot);break;case $tfoot.length&&settings.cloneHeadToFoot==false&&settings.footer==true:$divFoot.find("table").append($tfoot).css({"margin-top":-tableProps.border});helpers._setupClone($divFoot,tableProps.tfoot);break}},_getTableProps:function($obj){var tableProp={thead:{},tbody:{},tfoot:{},border:0},borderCollapse=1;if(settings.borderCollapse==true){borderCollapse=2}tableProp.border=($obj.find("th:first-child").outerWidth()-$obj.find("th:first-child").innerWidth())/borderCollapse;$obj.find("thead tr:first-child > *").each(function(index){tableProp.thead[index]=$(this).width()+tableProp.border});$obj.find("tfoot tr:first-child > *").each(function(index){tableProp.tfoot[index]=$(this).width()+tableProp.border});$obj.find("tbody tr:first-child > *").each(function(index){tableProp.tbody[index]=$(this).width()+tableProp.border});return tableProp},_setupClone:function($obj,cellArray){var $self=$obj,selector=$self.find("thead").length?"thead tr:first-child > *":$self.find("tfoot").length?"tfoot tr:first-child > *":"tbody tr:first-child > *",$cell;$self.find(selector).each(function(index){$cell=$(this).find("div.fht-cell").length?$(this).find("div.fht-cell"):$('
').appendTo($(this));$cell.css({width:parseInt(cellArray[index],10)});if(!$(this).closest(".fht-tbody").length&&$(this).is(":last-child")&&!$(this).closest(".fht-fixed-column").length){var padding=Math.max(($(this).innerWidth()-$(this).width())/2,settings.scrollbarOffset);$(this).css({"padding-right":parseInt($(this).css("padding-right"))+padding+"px"})}})},_isPaddingIncludedWithWidth:function(){var $obj=$('
test
'),defaultHeight,newHeight;$obj.addClass(settings.originalTable.attr("class"));$obj.appendTo("body");defaultHeight=$obj.find("td").height();$obj.find("td").css("height",$obj.find("tr").height());newHeight=$obj.find("td").height();$obj.remove();if(defaultHeight!=newHeight){return true}else{return false}},_getScrollbarWidth:function(){var scrollbarWidth=0;if(!scrollbarWidth){if(/msie/.test(navigator.userAgent.toLowerCase())){var $textarea1=$('').css({position:"absolute",top:-1e3,left:-1e3}).appendTo("body"),$textarea2=$('').css({position:"absolute",top:-1e3,left:-1e3}).appendTo("body");scrollbarWidth=$textarea1.width()-$textarea2.width()+2;$textarea1.add($textarea2).remove()}else{var $div=$("
").css({width:100,height:100,overflow:"auto",position:"absolute",top:-1e3,left:-1e3}).prependTo("body").append("
").find("div").css({width:"100%",height:200});scrollbarWidth=100-$div.width();$div.parent().remove()}}return scrollbarWidth}};if(methods[method]){return methods[method].apply(this,Array.prototype.slice.call(arguments,1))}else if(typeof method==="object"||!method){return methods.init.apply(this,arguments)}else{$.error('Method "'+method+'" does not exist in fixedHeaderTable plugin!')}}})(jQuery); \ No newline at end of file