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

21 lines
9.4 KiB
JavaScript

/*!
* jquery.fixedHeaderTable. The jQuery fixedHeaderTable plugin
*
* Copyright (c) 2013 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(d){d.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={},l={init:function(a){b=d.extend({},u,a);return this.each(function(){var a=d(this);h._isTable(a)?(l.setup.apply(this,Array.prototype.slice.call(arguments,1)),d.isFunction(b.create)&&b.create.call(this)):d.error("Invalid table mark-up")})},setup:function(){var a=d(this), c=a.find("thead"),e=a.find("tfoot"),g=0,f,k,n;b.originalTable=d(this).clone();b.includePadding=h._isPaddingIncludedWithWidth();b.scrollbarOffset=h._getScrollbarWidth();b.themeClassName=b.themeClass;f=-1<b.width.search("%")?a.parent().width()-b.scrollbarOffset:b.width-b.scrollbarOffset;a.css({width:f});a.closest(".fht-table-wrapper").length||(a.addClass("fht-table"),a.wrap('<div class="fht-table-wrapper"></div>'));f=a.closest(".fht-table-wrapper");!0==b.fixedColumn&&0>=b.fixedColumns&&(b.fixedColumns= 1);0<b.fixedColumns&&0==f.find(".fht-fixed-column").length&&(a.wrap('<div class="fht-fixed-body"></div>'),d('<div class="fht-fixed-column"></div>').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('<div class="fht-tbody"></div>');n=a.closest(".fht-tbody");var p=h._getTableProps(a);h._setupClone(n,p.tbody);a.hasClass("fht-table-init")?k=f.find("div.fht-thead"):(k=0<b.fixedColumns?d('<div class="fht-thead"><table class="fht-table"></table></div>').prependTo(k): d('<div class="fht-thead"><table class="fht-table"></table></div>').prependTo(f),k.find("table.fht-table").addClass(b.originalTable.attr("class")),c.clone().appendTo(k.find("table")));h._setupClone(k,p.thead);a.css({"margin-top":-k.outerHeight(!0)});!0==b.footer&&(h._setupTableFooter(a,this,p),e.length||(e=f.find("div.fht-tfoot table")),g=e.outerHeight(!0));c=f.height()-c.outerHeight(!0)-g-p.border;n.css({height:c});a.addClass("fht-table-init");"undefined"!==typeof b.altClass&&l.altRows.apply(this); 0<b.fixedColumns&&h._setupFixedColumn(a,this,p);b.autoShow||f.hide();h._bindScroll(n,p);return this},resize:function(){return this},altRows:function(a){var c=d(this);a="undefined"!==typeof a?a:b.altClass;c.closest(".fht-table-wrapper").find("tbody tr:odd:not(:hidden)").addClass(a)},show:function(a,c,b){var g=d(this),f=g.closest(".fht-table-wrapper");if("undefined"!==typeof a&&"number"===typeof a)return f.show(a,function(){d.isFunction(c)&&c.call(this)}),this;if("undefined"!==typeof a&&"string"=== typeof a&&"undefined"!==typeof c&&"number"===typeof c)return f.show(a,c,function(){d.isFunction(b)&&b.call(this)}),this;g.closest(".fht-table-wrapper").show();d.isFunction(a)&&a.call(this);return this},hide:function(a,c,b){var g=d(this),f=g.closest(".fht-table-wrapper");if("undefined"!==typeof a&&"number"===typeof a)return f.hide(a,function(){d.isFunction(b)&&b.call(this)}),this;if("undefined"!==typeof a&&"string"===typeof a&&"undefined"!==typeof c&&"number"===typeof c)return f.hide(a,c,function(){d.isFunction(b)&& b.call(this)}),this;g.closest(".fht-table-wrapper").hide();d.isFunction(b)&&b.call(this);return this},destroy:function(){var a=d(this),c=a.closest(".fht-table-wrapper");a.insertBefore(c).removeAttr("style").append(c.find("tfoot")).removeClass("fht-table fht-table-init").find(".fht-cell").remove();c.remove();return this}},h={_isTable:function(a){var c=a.is("table"),b=0<a.find("thead").length;a=0<a.find("tbody").length;return c&&b&&a?!0:!1},_bindScroll:function(a){var c=a.closest(".fht-table-wrapper"), d=a.siblings(".fht-thead"),g=a.siblings(".fht-tfoot");a.bind("scroll",function(){0<b.fixedColumns&&c.find(".fht-fixed-column").find(".fht-tbody table").css({"margin-top":-a.scrollTop()});d.find("table").css({"margin-left":-this.scrollLeft});(b.footer||b.cloneHeadToFoot)&&g.find("table").css({"margin-left":-this.scrollLeft})})},_fixHeightWithCss:function(a,c){b.includePadding?a.css({height:a.height()+c.border}):a.css({height:a.parent().height()+c.border})},_fixWidthWithCss:function(a,c,e){b.includePadding? a.each(function(){d(this).css({width:void 0==e?d(this).width()+c.border:e+c.border})}):a.each(function(){d(this).css({width:void 0==e?d(this).parent().width()+c.border:e+c.border})})},_setupFixedColumn:function(a,c,e){var g=a.closest(".fht-table-wrapper"),f=g.find(".fht-fixed-body");c=g.find(".fht-fixed-column");var k=d('<div class="fht-thead"><table class="fht-table"><thead><tr></tr></thead></table></div>'),n=d('<div class="fht-tbody"><table class="fht-table"><tbody></tbody></table></div>');a=d('<div class="fht-tfoot"><table class="fht-table"><tfoot><tr></tr></tfoot></table></div>'); var g=g.width(),p=f.find(".fht-tbody").height()-b.scrollbarOffset,q,m,l,r;k.find("table.fht-table").addClass(b.originalTable.attr("class"));n.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+")");l=b.fixedColumns*e.border;q.each(function(){l+=d(this).outerWidth(!0)});h._fixHeightWithCss(q,e);h._fixWidthWithCss(q,e);var s=[];q.each(function(){s.push(d(this).width())}); m=f.find("tbody tr > *:not(:nth-child(n+"+(b.fixedColumns+1)+"))").each(function(a){h._fixHeightWithCss(d(this),e);h._fixWidthWithCss(d(this),e,s[a%b.fixedColumns])});k.appendTo(c).find("tr").append(q.clone());n.appendTo(c).css({"margin-top":-1,height:p+e.border});m.each(function(a){0==a%b.fixedColumns&&(r=d("<tr></tr>").appendTo(n.find("tbody")),b.altClass&&d(this).parent().hasClass(b.altClass)&&r.addClass(b.altClass));d(this).clone().appendTo(r)});c.css({height:0,width:l});var t=c.find(".fht-tbody .fht-table").height()- c.find(".fht-tbody").height();c.find(".fht-tbody .fht-table").bind("mousewheel",function(a,c,b,e){if(0!=e)return a=parseInt(d(this).css("marginTop"),10)+(0<e?120:-120),0<a&&(a=0),a<-t&&(a=-t),d(this).css("marginTop",a),f.find(".fht-tbody").scrollTop(-a).scroll(),!1});f.css({width:g});if(!0==b.footer||!0==b.cloneHeadToFoot)k=f.find(".fht-tfoot tr > *:lt("+b.fixedColumns+")"),h._fixHeightWithCss(k,e),a.appendTo(c).find("tr").append(k.clone()),c=a.find("table").innerWidth(),a.css({top:b.scrollbarOffset, width:c})},_setupTableFooter:function(a,c,e){c=a.closest(".fht-table-wrapper");var g=a.find("tfoot");a=c.find("div.fht-tfoot");a.length||(a=0<b.fixedColumns?d('<div class="fht-tfoot"><table class="fht-table"></table></div>').appendTo(c.find(".fht-fixed-body")):d('<div class="fht-tfoot"><table class="fht-table"></table></div>').appendTo(c));a.find("table.fht-table").addClass(b.originalTable.attr("class"));switch(!0){case !g.length&&!0==b.cloneHeadToFoot&&!0==b.footer:e=c.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 c={thead:{},tbody:{},tfoot:{},border:0},e=1;!0==b.borderCollapse&&(e=2);c.border=(a.find("th:first-child").outerWidth()-a.find("th:first-child").innerWidth())/e;a.find("thead tr:first-child > *").each(function(a){c.thead[a]=d(this).width()+c.border});a.find("tfoot tr:first-child > *").each(function(a){c.tfoot[a]= d(this).width()+c.border});a.find("tbody tr:first-child > *").each(function(a){c.tbody[a]=d(this).width()+c.border});return c},_setupClone:function(a,c){var e=a.find("thead").length?"thead tr:first-child > *":a.find("tfoot").length?"tfoot tr:first-child > *":"tbody tr:first-child > *",g;a.find(e).each(function(a){g=d(this).find("div.fht-cell").length?d(this).find("div.fht-cell"):d('<div class="fht-cell"></div>').appendTo(d(this));g.css({width:parseInt(c[a],10)});d(this).closest(".fht-tbody").length|| !d(this).is(":last-child")||d(this).closest(".fht-fixed-column").length||(a=Math.max((d(this).innerWidth()-d(this).width())/2,b.scrollbarOffset),d(this).css({"padding-right":parseInt(d(this).css("padding-right"))+a+"px"}))})},_isPaddingIncludedWithWidth:function(){var a=d('<table class="fht-table"><tr><td style="padding: 10px; font-size: 10px;">test</td></tr></table>'),c,e;a.addClass(b.originalTable.attr("class"));a.appendTo("body");c=a.find("td").height();a.find("td").css("height",a.find("tr").height()); e=a.find("td").height();a.remove();return c!=e?!0:!1},_getScrollbarWidth:function(){var a=0;if(!a)if(/msie/.test(navigator.userAgent.toLowerCase())){var c=d('<textarea cols="10" rows="2"></textarea>').css({position:"absolute",top:-1E3,left:-1E3}).appendTo("body"),b=d('<textarea cols="10" rows="2" style="overflow: hidden;"></textarea>').css({position:"absolute",top:-1E3,left:-1E3}).appendTo("body"),a=c.width()-b.width()+2;c.add(b).remove()}else c=d("<div />").css({width:100,height:100,overflow:"auto", position:"absolute",top:-1E3,left:-1E3}).prependTo("body").append("<div />").find("div").css({width:"100%",height:200}),a=100-c.width(),c.parent().remove();return a}};if(l[m])return l[m].apply(this,Array.prototype.slice.call(arguments,1));if("object"!==typeof m&&m)d.error('Method "'+m+'" does not exist in fixedHeaderTable plugin!');else return l.init.apply(this,arguments)}})(jQuery);