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

19 lines
10 KiB
JavaScript

/*
* jquery.fixedHeaderTable. The jQuery fixedHeaderTable plugin
*
* Copyright (c) 2013 Mark Malek
* (c) 2015-2016 Vitaliy Filippov
* http://fixedheadertable.com
*
* Licensed under MIT
* http://www.opensource.org/licenses/mit-license.php
*
* http://docs.jquery.com/Plugins/Authoring
* jQuery authoring guidelines
*
* Version: 2016-08-10
* Based on: Original 1.3 (2011-05-09)
*
* 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,fixedColumns:0,fixedColumn:false,sortable:false,autoShow:true,footer:false,cloneHeadToFoot:false,autoResize:true,create:null};var c={};var b={init:function(g){c=a.extend({},e,g);return this.each(function(){var h=a(this);if(d._isTable(h)){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(){var r=a(this),s=this,m=r.find("thead"),p=r.find("tfoot"),n=0,k,o,j,i,g,q;c.originalTable=a(this).clone();c.includePadding=d._isPaddingIncludedWithWidth();c.scrollbarOffset=d._getScrollbarWidth();c.themeClassName=c.themeClass;if(c.width.search("%")>-1){g=r.parent().width()}else{g=c.width}q=g-c.scrollbarOffset;r.css({width:q});if(!r.closest(".fht-table-wrapper").length){r.addClass("fht-table");r.wrap('<div class="fht-table-wrapper"></div>')}k=r.closest(".fht-table-wrapper");if(c.fixedColumn==true&&c.fixedColumns<=0){c.fixedColumns=1}if(c.fixedColumns>0&&k.find(".fht-fixed-column").length==0){r.wrap('<div class="fht-fixed-body"></div>');a('<div class="fht-fixed-column"></div>').prependTo(k);i=k.find(".fht-fixed-body")}k.css({width:c.width,height:c.height}).addClass(c.themeClassName);if(!r.hasClass("fht-table-init")){r.wrap('<div class="fht-tbody"></div>')}j=r.closest(".fht-tbody");var h=d._getTableProps(r);d._setupClone(j,h.thead);if(!r.hasClass("fht-table-init")){if(c.fixedColumns>0){o=a('<div class="fht-thead"><table class="fht-table"></table></div>').prependTo(i)}else{o=a('<div class="fht-thead"><table class="fht-table"></table></div>').prependTo(k)}o.find("table.fht-table").addClass(c.originalTable.attr("class")).attr("style",c.originalTable.attr("style"));m.clone().appendTo(o.find("table"))}else{o=k.find(c.fixedColumns>0?"div.fht-fixed-body div.fht-thead":"div.fht-thead");o.find("thead").remove();m.clone().appendTo(o.find("table"))}o.find("table.fht-table").css({width:g});if(c.footer==true){d._setupTableFooter(r,s,h);if(!p.length){p=k.find("div.fht-tfoot table")}n=p.outerHeight(true)}var l=k.height()-m.outerHeight(true)-n-h.border;if(r.height()-o.outerHeight(true)<=l){o.find("table.fht-table").css({width:q});c.scrollbarOffset=0}d._setupClone(o,h.thead);r.css({"margin-top":-o.outerHeight(true)});j.css({height:l});if(c.autoResize&&!r.hasClass("fht-table-init")){a(window).on("resize",function(){k.find(".fht-cell").remove();r.fixedHeaderTable(c);b.resize.apply(s)})}r.addClass("fht-table-init");if(typeof(c.altClass)!=="undefined"){b.altRows.apply(s)}if(c.fixedColumns>0){d._setupFixedColumn(r,s,h)}if(!c.autoShow){k.hide()}d._bindScroll(j,h);return s},resize:function(){var g=a(this);g.closest(".fht-table-wrapper").find(".fht-cell").remove();g.fixedHeaderTable();return self},altRows:function(h){var i=a(this),g=(typeof(h)!=="undefined")?h:c.altClass;i.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(k){var j=k,i=j.closest(".fht-table-wrapper"),h=j.siblings(".fht-thead"),g=j.siblings(".fht-tfoot");j.bind("scroll",function(){if(c.fixedColumns>0){var l=i.find(".fht-fixed-column");l.find(".fht-tbody table").css({"margin-top":-j.scrollTop()})}h.find("table").css({"margin-left":-this.scrollLeft});if(c.footer||c.cloneHeadToFoot){g.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(i,g,h){if(c.includePadding){i.each(function(){a(this).css({width:h==undefined?a(this).width()+g.border:h+g.border})})}else{i.each(function(){a(this).css({width:h==undefined?a(this).parent().width()+g.border:h+g.border})})}},_setupFixedColumn:function(z,q,k){var y=z,x=y.closest(".fht-table-wrapper"),v=x.find(".fht-fixed-body"),o=x.find(".fht-fixed-column"),p=o.find(".fht-thead"),s=o.find(".fht-tbody"),g=o.find(".fht-tfoot"),i=x.width(),t=v.find(".fht-tbody").height()-c.scrollbarOffset,h,u,r,j,l;h=v.find(".fht-thead thead tr > *:lt("+c.fixedColumns+")");r=c.fixedColumns*k.border;h.each(function(){r+=a(this).outerWidth(true)});d._fixHeightWithCss(h,k);d._fixWidthWithCss(h,k);var n=[];h.each(function(){n.push(a(this).width())});l="tbody tr > *:not(:nth-child(n+"+(c.fixedColumns+1)+"))";u=v.find(l).each(function(B){d._fixHeightWithCss(a(this),k);d._fixWidthWithCss(a(this),k,n[B%c.fixedColumns])});if(!p[0]){p=a('<div class="fht-thead"><table class="fht-table '+c.originalTable.attr("class")+'"><thead><tr></tr></thead></table></div>');p.appendTo(o)}else{p.find("tr").html("")}p.find("tr").append(h.clone());if(!s[0]){s=a('<div class="fht-tbody"><table class="fht-table '+c.originalTable.attr("class")+'"><tbody></tbody></table></div>');s.appendTo(o)}else{s.find("tbody").html("")}s.css({"margin-top":-1,height:t+k.border});u.each(function(B){if(B%c.fixedColumns==0){j=a("<tr></tr>").appendTo(s.find("tbody"));if(c.altClass&&a(this).parent().hasClass(c.altClass)){j.addClass(c.altClass)}}a(this).clone().appendTo(j)});o.css({height:0,width:r});var A=o.find(".fht-tbody .fht-table").height()-o.find(".fht-tbody").height();o.find(".fht-tbody .fht-table").bind("mousewheel",function(D,F,C,B){if(B==0){return}var E=parseInt(a(this).css("marginTop"),10)+(B>0?120:-120);if(E>0){E=0}if(E<-A){E=-A}a(this).css("marginTop",E);v.find(".fht-tbody").scrollTop(-E).scroll();return false});v.css({width:i});if(c.footer==true||c.cloneHeadToFoot==true){var m=v.find(".fht-tfoot tr > *:lt("+c.fixedColumns+")"),w;d._fixHeightWithCss(m,k);if(!g[0]){g=a('<div class="fht-tfoot"><table class="fht-table '+c.originalTable.attr("class")+'"><tfoot><tr></tr></tfoot></table></div>');g.appendTo(o)}else{g.find("tr").html("")}g.find("tr").append(m.clone());w=g.find("table").innerWidth();g.css({top:c.scrollbarOffset,width:w})}},_setupTableFooter:function(n,l,g){var m=n,k=m.closest(".fht-table-wrapper"),h=m.find("tfoot"),j=k.find("div.fht-tfoot");if(!j.length){if(c.fixedColumns>0){j=a('<div class="fht-tfoot"><table class="fht-table"></table></div>').appendTo(k.find(".fht-fixed-body"))}else{j=a('<div class="fht-tfoot"><table class="fht-table"></table></div>').appendTo(k)}}j.find("table.fht-table").addClass(c.originalTable.attr("class"));switch(true){case !h.length&&c.cloneHeadToFoot==true&&c.footer==true:var i=k.find("div.fht-thead");j.empty();i.find("table").clone().appendTo(j);break;case h.length&&c.cloneHeadToFoot==false&&c.footer==true:j.find("table").append(h).css({"margin-top":-g.border});d._setupClone(j,g.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 > *").each(function(j){h.thead[j]=a(this).width()+h.border});i.find("tfoot tr:first-child > *").each(function(j){h.tfoot[j]=a(this).width()+h.border});i.find("tbody tr:first-child > *").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 tr:first-child > *":(i.find("tfoot").length)?"tfoot tr:first-child > *":"tbody tr:first-child > *",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],10)});if(!a(this).closest(".fht-tbody").length&&a(this).is(":last-child")&&!a(this).closest(".fht-fixed-column").length){a(this).css({"padding-right":parseInt(a(this).css("padding-right"))+c.scrollbarOffset+"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.addClass(c.originalTable.attr("class"));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(/msie/.test(navigator.userAgent.toLowerCase())){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);