Fixed-Header-Table/jquery.fixedheadertable.min.js

20 lines
8.5 KiB
JavaScript

/*!
* jquery.fixedHeaderTable. The jQuery fixedHeaderTable plugin
*
* Copyright (c) 2011 Mark Malek
* http://fixedheadertable.com
*
* Licensed under MIT
* http://www.opensource.org/licenses/mit-license.php
*
* http://docs.jquery.com/Plugins/Authoring
* jQuery authoring guidelines
*
* Launch : October 2009
* Version : 1.3
* Released: May 9th, 2011
*
*
* all CSS sizing (width,height) is done in pixels (px)
*/
(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('<div class="fht-table-wrapper"></div>')}u=w.closest(".fht-table-wrapper");if(c.fixedColumn==true&&u.find(".fht-fixed-column").length==0){w.wrap('<div class="fht-fixed-body"></div>');var o=a('<div class="fht-fixed-column"></div>').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('<div class="fht-tbody"></div>')}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('<div class="fht-thead"><table class="fht-table"></table></div>').prependTo(t)}else{s=a('<div class="fht-thead"><table class="fht-table"></table></div>').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('<div class="fht-thead"><table class="fht-table"><thead><tr></tr></thead></table></div>'),q=a('<div class="fht-tbody"><table class="fht-table"><tbody></tbody></table></div>'),g=a('<div class="fht-tfoot"><table class="fht-table"><thead><tr></tr></thead></table></div>'),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("<tr></tr>").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('<div class="fht-tfoot"><table class="fht-table"></table></div>').appendTo(j.find(".fht-fixed-body"))}else{n=a('<div class="fht-tfoot"><table class="fht-table"></table></div>').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('<div class="fht-cell"></div>').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('<table class="fht-table"><tr><td style="padding: 10px; font-size: 10px;">test</td></tr></table>'),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('<textarea cols="10" rows="2"></textarea>').css({position:"absolute",top:-1000,left:-1000}).appendTo("body"),i=a('<textarea cols="10" rows="2" style="overflow: hidden;"></textarea>').css({position:"absolute",top:-1000,left:-1000}).appendTo("body");h=j.width()-i.width()+2;j.add(i).remove()}else{var g=a("<div />").css({width:100,height:100,overflow:"auto",position:"absolute",top:-1000,left:-1000}).prependTo("body").append("<div />").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);