diff --git a/jquery.tablescroll.js b/jquery.tablescroll.js index 7596b3f..a2388cf 100644 --- a/jquery.tablescroll.js +++ b/jquery.tablescroll.js @@ -1,57 +1,49 @@ /* - -Copyright (c) 2009 Dimas Begunoff, http://www.farinspace.com - -Licensed under the MIT license -http://en.wikipedia.org/wiki/MIT_License - -Permission is hereby granted, free of charge, to any person -obtaining a copy of this software and associated documentation -files (the "Software"), to deal in the Software without -restriction, including without limitation the rights to use, -copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the -Software is furnished to do so, subject to the following -conditions: - -The above copyright notice and this permission notice shall be -included in all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES -OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT -HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, -WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR -OTHER DEALINGS IN THE SOFTWARE. - -*/ - -;(function($){ - + Copyright (c) 2009 Dimas Begunoff, http://www.farinspace.com + Licensed under the MIT license + http://en.wikipedia.org/wiki/MIT_License + Permission is hereby granted, free of charge, to any person + obtaining a copy of this software and associated documentation + files (the "Software"), to deal in the Software without + restriction, including without limitation the rights to use, + copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the + Software is furnished to do so, subject to the following + conditions: + The above copyright notice and this permission notice shall be + included in all copies or substantial portions of the Software. + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES + OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT + HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + OTHER DEALINGS IN THE SOFTWARE. + */ +; +(function ($) { var scrollbarWidth = 0; - - // http://jdsharp.us/jQuery/minute/calculate-scrollbar-width.php - function getScrollbarWidth() +// http://jdsharp.us/jQuery/minute/calculate-scrollbar-width.php + function getScrollbarWidth() { - if (scrollbarWidth) return scrollbarWidth; - var div = $('
'); - $('body').append(div); - var w1 = $('div', div).innerWidth(); - div.css('overflow-y', 'auto'); - var w2 = $('div', div).innerWidth(); - $(div).remove(); + if (scrollbarWidth) + return scrollbarWidth; + var div = $(''); + $('body').append(div); + var w1 = $('div', div).outerWidth(); + div.css('overflow-y', 'auto'); + var w2 = $('div', div).outerWidth(); + $(div).remove(); scrollbarWidth = (w1 - w2); return scrollbarWidth; } - - $.fn.tableScroll = function(options) + $.fn.tableScroll = function (options) { if (options == 'undo') { var container = $(this).parent().parent(); - if (container.hasClass('tablescroll_wrapper')) + if (container.hasClass('tablescroll_wrapper')) { container.find('.tablescroll_head thead').prependTo(this); container.find('.tablescroll_foot tfoot').appendTo(this); @@ -60,129 +52,121 @@ OTHER DEALINGS IN THE SOFTWARE. } return; } - - var settings = $.extend({},$.fn.tableScroll.defaults,options); - + var settings = $.extend({}, $.fn.tableScroll.defaults, options); // Bail out if there's no vertical overflow //if ($(this).height() <= settings.height) //{ - // return this; + // return this; //} - settings.scrollbarWidth = getScrollbarWidth(); - - this.each(function() + this.each(function () { var flush = settings.flush; - + var oriClass = $(this).attr('class'); var tb = $(this).addClass('tablescroll_body'); - - // find or create the wrapper div (allows tableScroll to be re-applied) - var wrapper; - if (tb.parent().hasClass('tablescroll_wrapper')) { - wrapper = tb.parent(); - } - else { - wrapper = $('').insertBefore(tb).append(tb); - } - - // check for a predefined container +// find or create the wrapper div (allows tableScroll to be re-applied) + var wrapper; + if (tb.parent().hasClass('tablescroll_wrapper')) { + wrapper = tb.parent(); + } + else { + wrapper = $('').insertBefore(tb).append(tb); + } +// check for a predefined container if (!wrapper.parent('div').hasClass(settings.containerClass)) { $('').addClass(settings.containerClass).insertBefore(wrapper).append(wrapper); } - var width = settings.width ? settings.width : tb.outerWidth(); - wrapper.css - ({ - 'width': width+'px', - 'height': settings.height+'px', - 'overflow': 'auto' - }); - - tb.css('width',width+'px'); - + ({ + 'width': width + 'px', + 'height': settings.height + 'px', + 'overflow': 'auto' + }); + tb.css('width', width + 'px'); // with border difference var wrapper_width = wrapper.outerWidth(); - var diff = wrapper_width-width; - + var diff = wrapper_width - width; // assume table will scroll - wrapper.css({width:((width-diff)+settings.scrollbarWidth)+'px'}); - tb.css('width',(width-diff)+'px'); - + wrapper.css({width: ((width - diff) + settings.scrollbarWidth) + 'px'}); + tb.css('width', (width - diff) + 'px'); if (tb.outerHeight() <= settings.height) { - wrapper.css({height:'auto',width:(width-diff)+'px'}); + wrapper.css({height: 'auto', width: (width - diff) + 'px'}); flush = false; } - - // using wrap does not put wrapper in the DOM right + // using wrap does not put wrapper in the DOM right // away making it unavailable for use during runtime // tb.wrap(wrapper); - // possible speed enhancements - var has_thead = $('thead',tb).length ? true : false ; - var has_tfoot = $('tfoot',tb).length ? true : false ; - var thead_tr_first = $('thead tr:first',tb); - var tbody_tr_first = $('tbody tr:first',tb); - var tfoot_tr_first = $('tfoot tr:first',tb); - + var has_thead = $('thead', tb).length ? true : false; + var has_tfoot = $('tfoot', tb).length ? true : false; + var thead_tr_first = $('thead tr:first', tb); + var tbody_tr_first = $('tbody tr:first', tb); + var tfoot_tr_first = $('tfoot tr:first', tb); + var tbody_tr = $('tbody tr', tb); // remember width of last cell var w = 0; - - $('th, td',thead_tr_first).each(function(i) - { - w = $(this).width(); - - $('th:eq('+i+'), td:eq('+i+')',thead_tr_first).css('width',w+'px'); - $('th:eq('+i+'), td:eq('+i+')',tbody_tr_first).css('width',w+'px'); - if (has_tfoot) $('th:eq('+i+'), td:eq('+i+')',tfoot_tr_first).css('width',w+'px'); + var aw = new Array(); + $('th, td', tbody_tr_first).each(function (i){ + aw[i] = $(this).width(); }); - - if (has_thead) + + if (has_thead) { - var tbh = $('