forked from markmalek/Fixed-Header-Table
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.fixedheadertable.min.js
37 lines (37 loc) · 9.07 KB
/
jquery.fixedheadertable.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/*!
* 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(c){c.fn.fixedHeaderTable=function(h){var b={},t={width:"100%",height:"100%",themeClass:"fht-default",altClass:void 0,borderCollapse:!0,fixedColumns:0,fixedColumn:!1,sortable:!1,autoShow:!0,footer:!1,cloneHeadToFoot:!1,create:null},i={_isTable:function(a){var d=a.is("table"),b=0<a.find("thead").length,a=0<a.find("tbody").length;return d&&b&&a},_bindScroll:function(a){var d=a.closest(".fht-table-wrapper"),c=a.siblings(".fht-thead"),f=a.siblings(".fht-tfoot");a.bind("scroll",function(){0<b.fixedColumns&&
d.find(".fht-fixed-column").find(".fht-tbody table").css({"margin-top":-a.scrollTop()});c.find("table").css({"margin-left":-this.scrollLeft});(b.footer||b.cloneHeadToFoot)&&f.find("table").css({"margin-left":-this.scrollLeft})})},_fixHeightWithCss:function(a,d){b.includePadding?a.css({height:Math.round(a.height()+d.border)}):a.css({height:Math.round(a.parent().height()+d.border)})},_fixWidthWithCss:function(a,d,e){b.includePadding?a.each(function(){c(this).css({width:Math.round(void 0===e?c(this).width()+
d.border:e+d.border)})}):a.each(function(){c(this).css({width:Math.round(void 0===e?c(this).parent().width()+d.border:e+d.border)})})},_setupFixedColumn:function(a,d){var e=a.closest(".fht-table-wrapper"),f=e.find(".fht-fixed-body"),g=e.find(".fht-fixed-column"),n=c('<div class="fht-thead"><table class="fht-table"><thead><tr></tr></thead></table></div>'),m=c('<div class="fht-tbody"><table class="fht-table"><tbody></tbody></table></div>'),k=c('<div class="fht-tfoot"><table class="fht-table"><tfoot><tr></tr></tfoot></table></div>'),
j,h,l=e.width(),p=f.find(".fht-tbody").height()-b.scrollbarOffset,q;n.find("table.fht-table").addClass(b.originalTable.attr("class"));m.find("table.fht-table").addClass(b.originalTable.attr("class"));k.find("table.fht-table").addClass(b.originalTable.attr("class"));e=f.find(".fht-thead thead tr > *:lt("+b.fixedColumns+")");h=b.fixedColumns*d.border;e.each(function(){h+=c(this).outerWidth(!0)});i._fixHeightWithCss(e,d);i._fixWidthWithCss(e,d);var r=[];e.each(function(){r.push(c(this).width())});j=
f.find("tbody tr > *:not(:nth-child(n+"+(b.fixedColumns+1)+"))").each(function(a){i._fixHeightWithCss(c(this),d);i._fixWidthWithCss(c(this),d,r[a%b.fixedColumns])});n.appendTo(g).find("tr").append(e.clone());m.appendTo(g).css({"margin-top":-1,height:Math.round(p+d.border)});j.each(function(a){0===a%b.fixedColumns&&(q=c("<tr></tr>").appendTo(m.find("tbody")),b.altClass&&c(this).parent().hasClass(b.altClass)&&q.addClass(b.altClass));c(this).clone().appendTo(q)});g.css({height:0,width:Math.round(h)});
var s=g.find(".fht-tbody .fht-table").height()-g.find(".fht-tbody").height();g.find(".fht-table").bind("mousewheel",function(a,d,b,e){return 0!==e?(a=parseInt(c(this).css("marginTop"),10)+(0<e?120:-120),0<a&&(a=0),a<-s&&(a=-s),c(this).css("marginTop",a),f.find(".fht-tbody").scrollTop(-a).scroll(),!1):!0});f.css({width:Math.round(l)});if(!0===b.footer||!0===b.cloneHeadToFoot)n=f.find(".fht-tfoot tr > *:lt("+b.fixedColumns+")"),i._fixHeightWithCss(n,d),k.appendTo(g).find("tr").append(n.clone()),k.css({top:b.scrollbarOffset,
width:Math.round(k.find("table").innerWidth())})},_setupTableFooter:function(a,d){var e=a.closest(".fht-table-wrapper"),f=a.find("tfoot"),g=e.find("div.fht-tfoot");g.length||(g=c('<div class="fht-tfoot"><table class="fht-table"></table></div>'),0<b.fixedColumns?g.appendTo(e.find(".fht-fixed-body")):g.appendTo(e));g.find("table.fht-table").addClass(b.originalTable.attr("class"));!f.length&&!0===b.cloneHeadToFoot&&!0===b.footer?(e=e.find("div.fht-thead"),g.empty(),e.find("table").clone().appendTo(g)):
f.length&&(!1===b.cloneHeadToFoot&&!0===b.footer)&&(g.find("table").append(f).css({"margin-top":-d.border}),i._setupClone(g,d.tfoot))},_getTableProps:function(a){var d={thead:{},tbody:{},tfoot:{},border:0},e=1;!0===b.borderCollapse&&(e=2);d.border=(a.find("th:first-child").outerWidth()-a.find("th:first-child").innerWidth())/e;a.find("thead tr:first-child > *").each(function(a){d.thead[a]=c(this).width()+d.border});a.find("tfoot tr:first-child > *").each(function(a){d.tfoot[a]=c(this).width()+d.border});
a.find("tbody tr:first-child > *").each(function(a){d.tbody[a]=c(this).width()+d.border});return d},_setupClone:function(a,d){var e=a.find("thead").length?"thead tr:first-child > *":a.find("tfoot").length?"tfoot tr:first-child > *":"tbody tr:first-child > *",f;a.find(e).each(function(a){f=c(this).find("div.fht-cell").length?c(this).find("div.fht-cell"):c('<div class="fht-cell"></div>').appendTo(c(this));f.css({width:parseInt(d[a],10)});!c(this).closest(".fht-tbody").length&&(c(this).is(":last-child")&&
!c(this).closest(".fht-fixed-column").length)&&(a=(c(this).innerWidth()-c(this).width())/2+b.scrollbarOffset,c(this).css({"padding-right":a+"px"}))})},_isPaddingIncludedWithWidth:function(){var a=c('<table class="fht-table"><tr><td style="padding: 10px; font-size: 10px;">test</td></tr></table>'),d,e;a.addClass(b.originalTable.attr("class"));a.appendTo("body");d=a.find("td").height();a.find("td").css("height",Math.round(a.find("tr").height()));e=a.find("td").height();a.remove();return d!==e},_getScrollbarWidth:function(){var a=
0;if(!a)if(c.browser.msie){var d=c('<textarea cols="10" rows="2"></textarea>').css({position:"absolute",top:-1E3,left:-1E3}).appendTo("body"),b=c('<textarea cols="10" rows="2" style="overflow: hidden;"></textarea>').css({position:"absolute",top:-1E3,left:-1E3}).appendTo("body"),a=d.width()-b.width()+2;d.add(b).remove()}else d=c("<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-d.width(),d.parent().remove();return a}},l={init:function(a){b=c.extend({},t,a);return this.each(function(){var a=c(this);i._isTable(a)?(l.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"),f,g,h,m,k=0;b.originalTable=c(this).clone();b.includePadding=i._isPaddingIncludedWithWidth();b.scrollbarOffset=i._getScrollbarWidth();b.themeClassName=
b.themeClass;f=-1<b.width.search("%")?a.parent().width()-b.scrollbarOffset:b.width-b.scrollbarOffset;a.css({width:Math.round(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>'),m=f.find(".fht-fixed-body"));f.css({width:Math.round(b.width),
height:Math.round(b.height)}).addClass(b.themeClassName);a.hasClass("fht-table-init")||a.wrap('<div class="fht-tbody"></div>');h=a.closest(".fht-tbody");var j=i._getTableProps(a);i._setupClone(h,j.tbody);a.hasClass("fht-table-init")?g=f.find("div.fht-thead"):(g=c('<div class="fht-thead"><table class="fht-table"></table></div>'),0<b.fixedColumns?g.prependTo(m):g.prependTo(f),g.find("table.fht-table").addClass(b.originalTable.attr("class")),d.clone().appendTo(g.find("table")));i._setupClone(g,j.thead);
a.css({"margin-top":-Math.round(g.outerHeight(!0))});!0===b.footer&&(i._setupTableFooter(a,j),e.length||(e=f.find("div.fht-tfoot table")),k=e.outerHeight(!0));d=f.height()-d.outerHeight(!0)-k-j.border;h.css({height:Math.round(d)});a.addClass("fht-table-init");void 0!==b.altClass&&l.altRows.apply(this);0<b.fixedColumns&&i._setupFixedColumn(a,j);b.autoShow||f.hide();i._bindScroll(h);return this},altRows:function(a){var d=c(this),a=void 0!==a?a:b.altClass;d.closest(".fht-table-wrapper").find("tbody tr:odd:not(:hidden)").addClass(a)},
show:function(a,d,b){var f=c(this),g=f.closest(".fht-table-wrapper");void 0!==a&&"number"===typeof a?g.show(a,function(){c.isFunction(d)&&d.call(this)}):void 0!==a&&"string"===typeof a&&void 0!==d&&"number"===typeof d?g.show(a,d,function(){c.isFunction(b)&&b.call(this)}):(f.closest(".fht-table-wrapper").show(),c.isFunction(a)&&a.call(this));return this},hide:function(a,b,e){var f=c(this),g=f.closest(".fht-table-wrapper");void 0!==a&&"number"===typeof a?g.hide(a,function(){c.isFunction(e)&&e.call(this)}):
void 0!==a&&"string"===typeof a&&void 0!==b&&"number"===typeof b?g.hide(a,b,function(){c.isFunction(e)&&e.call(this)}):(f.closest(".fht-table-wrapper").hide(),c.isFunction(e)&&e.call(this));return this},destroy:function(){var a=c(this),b=a.closest(".fht-table-wrapper");a.insertBefore(b).removeAttr("style").append(b.find("tfoot")).removeClass("fht-table fht-table-init").find(".fht-cell").remove();b.remove();return this}},p;l[h]?p=l[h].apply(this,Array.prototype.slice.call(arguments,1)):"object"===
typeof h||!h?p=l.init.apply(this,arguments):c.error('Method "'+h+'" does not exist in fixedHeaderTable plugin!');return p}})(jQuery);