From c30fe94be0e30995c74dd681e99b290b64c5d86f Mon Sep 17 00:00:00 2001 From: Mark Malek Date: Sat, 28 May 2011 02:47:35 -0400 Subject: [PATCH] Minor refactoring Updated minified version Created a new public method for adding alt rows css class names --- jquery.fixedheadertable.js | 53 +++++++++++++++++++++++----------- jquery.fixedheadertable.min.js | 2 +- 2 files changed, 37 insertions(+), 18 deletions(-) diff --git a/jquery.fixedheadertable.js b/jquery.fixedheadertable.js index bb59cd1..aed7e15 100644 --- a/jquery.fixedheadertable.js +++ b/jquery.fixedheadertable.js @@ -25,18 +25,19 @@ // plugin's default options var defaults = { - width: '100%', - height: '100%', - borderCollapse: true, - themeClass: 'fht-default', + width: '100%', + height: '100%', + themeClass: 'fht-default', - fixedColumn: false, // fixed first column - autoShow: true, // hide table after its created - loader: false, - footer: false, // show footer - cloneHeadToFoot: false, // clone head and use as footer - autoResize: false, // resize table if its parent wrapper changes size - create: null // callback after plugin completes + borderCollapse: true, + fixedColumn: false, // fixed first column + sortable: false, + autoShow: true, // hide table after its created + footer: false, // show footer + cloneHeadToFoot: false, // clone head and use as footer + autoResize: false, // resize table if its parent wrapper changes size + + create: null // callback after plugin completes } @@ -172,8 +173,7 @@ $self.addClass('fht-table-init'); if ( typeof(settings.altClass) !== 'undefined' ) { - $self.find('tbody tr:odd') - .addClass(settings.altClass); + methods.altRows.apply( self ); } if ( settings.fixedColumn == true ) { @@ -200,6 +200,19 @@ return self; }, + /* + * Add CSS class to alternating rows + */ + altRows: function( arg1 ) { + var $self = $(this), + self = this, + altClass = ( typeof(arg1) !== 'undefined' ) ? arg1 : settings.altClass; + + $self.closest('.fht-table-wrapper') + .find('tbody tr:odd:not(:hidden)') + .addClass(altClass); + }, + /* * Show a hidden fixedHeaderTable table */ @@ -367,9 +380,15 @@ * return void */ _fixWidthWithCss: function( $obj, tableProps ) { - $obj.css({ - 'width': $obj.width() + tableProps.border - }); + if ( settings.includePadding ) { + $obj.css({ + 'width': $obj.width() + tableProps.border + }); + } else { + $obj.css({ + 'width': $obj.parent().width() + tableProps.border + }); + } }, /* @@ -440,7 +459,7 @@ .find('tr') .append($firstTdFootChild.clone()); $tfoot.css({ - 'top': settings.scrollbarOffset + tableProps.border + 'top': settings.scrollbarOffset }); } }, diff --git a/jquery.fixedheadertable.min.js b/jquery.fixedheadertable.min.js index 49032cd..fe771d8 100644 --- a/jquery.fixedheadertable.min.js +++ b/jquery.fixedheadertable.min.js @@ -17,4 +17,4 @@ * * all CSS sizing (width,height) is done in pixels (px) */ -(function(a){a.fn.fixedHeaderTable=function(f){var e={width:"100%",height:"100%",borderCollapse:true,themeClass:"fht-default",fixedColumn:false,autoShow:true,loader:false,footer:false,cloneHeadToFoot:false,autoResize:false,create:null};var c={};var b={init:function(g){c=a.extend({},e,g);return this.each(function(){var i=a(this),h=this;if(d._isTable(i)){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(j){var w=a(this),q=this,p=w.find("thead"),h=w.find("tfoot"),r=w.find("tbody"),u,s,n,x,k,g,v=0;c.includePadding=d._isPaddingIncludedWithWidth();c.scrollbarOffset=d._getScrollbarWidth();c.themeClassName=c.themeClass;if(c.width.search("%")>-1){var i=w.parent().width()-c.scrollbarOffset}else{var i=c.width-c.scrollbarOffset}w.css({width:i});if(!w.closest(".fht-table-wrapper").length){w.addClass("fht-table");w.wrap('
')}u=w.closest(".fht-table-wrapper");if(c.fixedColumn==true&&u.find(".fht-fixed-column").length==0){w.wrap('
');var o=a('
').prependTo(u),t=u.find(".fht-fixed-body")}u.css({width:c.width,height:c.height}).addClass(c.themeClassName);if(!w.hasClass("fht-table-init")){w.wrap('
')}x=w.closest(".fht-tbody");var l=d._getTableProps(w);d._setupClone(x,l.tbody);if(!w.hasClass("fht-table-init")){if(c.fixedColumn==true){s=a('
').prependTo(t)}else{s=a('
').prependTo(u)}p.clone().appendTo(s.find("table"))}else{s=u.find("div.fht-thead")}d._setupClone(s,l.thead);w.css({"margin-top":-s.outerHeight(true)});if(c.footer==true){d._setupTableFooter(w,q,l);if(!h.length){h=u.find("div.fht-tfoot table")}v=h.outerHeight(true)}var m=u.height()-p.outerHeight(true)-v-l.border;x.css({height:m});w.addClass("fht-table-init");if(typeof(c.altClass)!=="undefined"){w.find("tbody tr:odd").addClass(c.altClass)}if(c.fixedColumn==true){d._setupFixedColumn(w,q,l)}if(!c.autoShow){u.hide()}d._bindScroll(x,l);return q},resize:function(h){var i=a(this),g=this;return 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(l,g){var k=l,j=k.closest(".fht-table-wrapper"),i=k.siblings(".fht-thead"),h=k.siblings(".fht-tfoot");k.bind("scroll",function(){if(c.fixedColumn==true){var m=j.find(".fht-fixed-column");m.find(".fht-tbody table").css({"margin-top":-k.scrollTop()})}i.find("table").css({"margin-left":-this.scrollLeft});if(c.cloneHeadToFoot){h.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(h,g){h.css({width:h.width()+g.border})},_setupFixedColumn:function(x,n,j){var v=x,p=n,u=v.closest(".fht-table-wrapper"),t=u.find(".fht-fixed-body"),l=u.find(".fht-fixed-column"),m=a('
'),q=a('
'),g=a('
'),w=t.find(".fht-thead thead tr th:first-child"),s,o=w.outerWidth(true)+j.border,h=u.width(),r=t.find(".fht-tbody").height()-c.scrollbarOffset,i;d._fixHeightWithCss(w,j);d._fixWidthWithCss(w,j);s=t.find("tbody tr td:first-child").each(function(y){d._fixHeightWithCss(a(this),j);d._fixWidthWithCss(a(this),j)});m.appendTo(l).find("tr").append(w.clone());q.appendTo(l).css({"margin-top":-1,height:r+j.border});s.each(function(y){i=a("").appendTo(q.find("tbody"));if(c.altClass&&a(this).parent().hasClass(c.altClass)){i.addClass(c.altClass)}a(this).clone().appendTo(i)});l.css({width:o});t.css({width:h});if(c.footer==true||c.cloneHeadToFoot==true){var k=t.find(".fht-tfoot thead tr th:first-child");d._fixHeightWithCss(k,j);g.appendTo(l).find("tr").append(k.clone());g.css({top:c.scrollbarOffset+j.border})}},_setupTableFooter:function(g,i,h){var m=g,o=i,j=m.closest(".fht-table-wrapper"),l=m.find("tfoot"),n=j.find("div.fht-tfoot");if(!n.length){if(c.fixedColumn==true){n=a('
').appendTo(j.find(".fht-fixed-body"))}else{n=a('
').appendTo(j)}}switch(true){case !l.length&&c.cloneHeadToFoot==true&&c.footer==true:var k=j.find("div.fht-thead");n.empty();k.find("table").clone().appendTo(n);break;case l.length&&c.cloneHeadToFoot==false&&c.footer==true:n.find("table").append(l).css({"margin-top":-h.border});d._setupClone(n,h.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 th").each(function(j){h.thead[j]=a(this).width()+h.border});i.find("tfoot tr:first-child td").each(function(j){h.tfoot[j]=a(this).width()+h.border});i.find("tbody tr:first-child td").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 th":(i.find("tfoot").length)?"tfoot td":"tbody td",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])});if(!a(this).closest(".fht-tbody").length&&a(this).is(":last-child")&&!a(this).closest(".fht-fixed-column").length){var m=((a(this).innerWidth()-a(this).width())/2)+c.scrollbarOffset;a(this).css({"padding-right":m+"px"})}})},_isPaddingIncludedWithWidth:function(){var i=a('
test
'),h,g;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(a.browser.msie){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 +(function(a){a.fn.fixedHeaderTable=function(f){var e={width:"100%",height:"100%",themeClass:"fht-default",borderCollapse:true,fixedColumn:false,sortable:false,autoShow:true,footer:false,cloneHeadToFoot:false,autoResize:false,create:null};var c={};var b={init:function(g){c=a.extend({},e,g);return this.each(function(){var i=a(this),h=this;if(d._isTable(i)){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(j){var w=a(this),q=this,p=w.find("thead"),h=w.find("tfoot"),r=w.find("tbody"),u,s,n,x,k,g,v=0;c.includePadding=d._isPaddingIncludedWithWidth();c.scrollbarOffset=d._getScrollbarWidth();c.themeClassName=c.themeClass;if(c.width.search("%")>-1){var i=w.parent().width()-c.scrollbarOffset}else{var i=c.width-c.scrollbarOffset}w.css({width:i});if(!w.closest(".fht-table-wrapper").length){w.addClass("fht-table");w.wrap('
')}u=w.closest(".fht-table-wrapper");if(c.fixedColumn==true&&u.find(".fht-fixed-column").length==0){w.wrap('
');var o=a('
').prependTo(u),t=u.find(".fht-fixed-body")}u.css({width:c.width,height:c.height}).addClass(c.themeClassName);if(!w.hasClass("fht-table-init")){w.wrap('
')}x=w.closest(".fht-tbody");var l=d._getTableProps(w);d._setupClone(x,l.tbody);if(!w.hasClass("fht-table-init")){if(c.fixedColumn==true){s=a('
').prependTo(t)}else{s=a('
').prependTo(u)}p.clone().appendTo(s.find("table"))}else{s=u.find("div.fht-thead")}d._setupClone(s,l.thead);w.css({"margin-top":-s.outerHeight(true)});if(c.footer==true){d._setupTableFooter(w,q,l);if(!h.length){h=u.find("div.fht-tfoot table")}v=h.outerHeight(true)}var m=u.height()-p.outerHeight(true)-v-l.border;x.css({height:m});w.addClass("fht-table-init");if(typeof(c.altClass)!=="undefined"){b.altRows.apply(q)}if(c.fixedColumn==true){d._setupFixedColumn(w,q,l)}if(!c.autoShow){u.hide()}d._bindScroll(x,l);return q},resize:function(h){var i=a(this),g=this;return g},altRows:function(i){var j=a(this),h=this,g=(typeof(i)!=="undefined")?i:c.altClass;j.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(l,g){var k=l,j=k.closest(".fht-table-wrapper"),i=k.siblings(".fht-thead"),h=k.siblings(".fht-tfoot");k.bind("scroll",function(){if(c.fixedColumn==true){var m=j.find(".fht-fixed-column");m.find(".fht-tbody table").css({"margin-top":-k.scrollTop()})}i.find("table").css({"margin-left":-this.scrollLeft});if(c.cloneHeadToFoot){h.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(h,g){if(c.includePadding){h.css({width:h.width()+g.border})}else{h.css({width:h.parent().width()+g.border})}},_setupFixedColumn:function(x,n,j){var v=x,p=n,u=v.closest(".fht-table-wrapper"),t=u.find(".fht-fixed-body"),l=u.find(".fht-fixed-column"),m=a('
'),q=a('
'),g=a('
'),w=t.find(".fht-thead thead tr th:first-child"),s,o=w.outerWidth(true)+j.border,h=u.width(),r=t.find(".fht-tbody").height()-c.scrollbarOffset,i;d._fixHeightWithCss(w,j);d._fixWidthWithCss(w,j);s=t.find("tbody tr td:first-child").each(function(y){d._fixHeightWithCss(a(this),j);d._fixWidthWithCss(a(this),j)});m.appendTo(l).find("tr").append(w.clone());q.appendTo(l).css({"margin-top":-1,height:r+j.border});s.each(function(y){i=a("").appendTo(q.find("tbody"));if(c.altClass&&a(this).parent().hasClass(c.altClass)){i.addClass(c.altClass)}a(this).clone().appendTo(i)});l.css({width:o});t.css({width:h});if(c.footer==true||c.cloneHeadToFoot==true){var k=t.find(".fht-tfoot thead tr th:first-child");d._fixHeightWithCss(k,j);g.appendTo(l).find("tr").append(k.clone());g.css({top:c.scrollbarOffset})}},_setupTableFooter:function(g,i,h){var m=g,o=i,j=m.closest(".fht-table-wrapper"),l=m.find("tfoot"),n=j.find("div.fht-tfoot");if(!n.length){if(c.fixedColumn==true){n=a('
').appendTo(j.find(".fht-fixed-body"))}else{n=a('
').appendTo(j)}}switch(true){case !l.length&&c.cloneHeadToFoot==true&&c.footer==true:var k=j.find("div.fht-thead");n.empty();k.find("table").clone().appendTo(n);break;case l.length&&c.cloneHeadToFoot==false&&c.footer==true:n.find("table").append(l).css({"margin-top":-h.border});d._setupClone(n,h.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 th").each(function(j){h.thead[j]=a(this).width()+h.border});i.find("tfoot tr:first-child td").each(function(j){h.tfoot[j]=a(this).width()+h.border});i.find("tbody tr:first-child td").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 th":(i.find("tfoot").length)?"tfoot td":"tbody td",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])});if(!a(this).closest(".fht-tbody").length&&a(this).is(":last-child")&&!a(this).closest(".fht-fixed-column").length){var m=((a(this).innerWidth()-a(this).width())/2)+c.scrollbarOffset;a(this).css({"padding-right":m+"px"})}})},_isPaddingIncludedWithWidth:function(){var i=a('
test
'),h,g;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(a.browser.msie){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