Guest User

Untitled

a guest
Jan 23rd, 2018
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.48 KB | None | 0 0
  1. /**
  2. * Make table body scrollable, with the table header always visible.
  3. * If no min/max-height is given, table shrinks to fit the browser viewport.
  4. *
  5. * requirements:
  6. * - jQuery framework required (tested with 1.6.2)
  7. * - header must be wrapped in <thead> element
  8. *
  9. * warnings:
  10. * - table <caption> is not supported
  11. * - using with header including <input>s is not adviced
  12. *
  13. * @author Vlasta Neubauer [paranoiq@centrum.cz]
  14. * @license public domain
  15. *
  16. * @param object table element (DOM or jQuery)
  17. * @param int minimal table height (optional)
  18. * @param int maximal table height (optional)
  19. * @return object table container
  20. */
  21. function makeScrollable(table, minHeight, maxHeight) {
  22. var $ = jQuery;
  23.  
  24. // fuck IE7 and older
  25. if ($.browser.msie && parseInt($.browser.version) < 8) return;
  26.  
  27. table = $(table);
  28.  
  29. // scrolling container
  30. table.wrap('<div>');
  31. var container = table.parent();
  32. container.css({ 'display': 'inline-block', 'border': 'none', 'padding': 0 });
  33.  
  34. container.attr('class', table.attr('class'));
  35.  
  36. container.css({ // cannot copy 'margin' (IE, FF, Webkit)
  37. 'marginTop': table.css('marginTop'), 'marginRight': table.css('marginRight'),
  38. 'marginBottom': table.css('marginBottom'), 'marginLeft': table.css('marginLeft') });
  39. table.css({ 'margin': 0 });
  40.  
  41. var bottom = table;
  42. if (!parseInt(bottom.css('borderBottomWidth')))
  43. var bottom = table.find("tr:last").find("th, td");
  44. container.css({ // cannot copy 'borderBottom' (IE, FF)
  45. 'borderBottomWidth': bottom.css('borderBottomWidth'),
  46. 'borderBottomColor': bottom.css('borderBottomColor'),
  47. 'borderBottomStyle': bottom.css('borderBottomStyle') });
  48. bottom.css({ 'borderBottom': 'none' });
  49.  
  50. // fake header
  51. var head = table.find("thead").clone(true);
  52. head.css({ 'display': 'block', 'position': 'absolute' });
  53. head.find("th:not(:last-child), td:not(:last-child)").css({ 'borderRight': 'none' });
  54. head.find("tr:not(:last-child)").find("th, td").css({ 'borderBottom': 'none' });
  55. head.insertBefore(table);
  56.  
  57. var origCells = table.find("thead tr:first-child").find("th, td");
  58. var copyCells = head.find("tr:first-child").find("th, td");
  59.  
  60. $(window).resize(function (event) {
  61. var oldPosition = container.scrollTop();
  62. container.scrollTop(0); // FF looses position of header on refresh
  63. head.css({ 'display': 'none', 'position': 'static' });
  64.  
  65. container.css({ 'maxHeight': 32000, 'overflowY': 'visible' }); // "maxHeight: auto" won't work
  66.  
  67. var tableHeight = table.outerHeight(true);
  68. var containerHeight = tableHeight
  69. - ($(document.body).outerHeight(true) - document.documentElement.clientHeight);
  70. if ($.browser.opera || ($.browser.msie && parseInt($.browser.version) < 9)) containerHeight -= 4; // WTF?
  71. if (maxHeight && containerHeight > maxHeight) containerHeight = maxHeight;
  72.  
  73. if (containerHeight >= tableHeight) {
  74. container.css({ 'minHeight': 0 });
  75. return;
  76. } else {
  77. if (minHeight) container.css({ 'minHeight': minHeight });
  78. container.css({ 'maxHeight': containerHeight, 'overflowY': 'scroll' });
  79. }
  80.  
  81. // sync column widths
  82. origCells.each(function (n, th) {
  83. if ($.browser.msie && parseInt($.browser.version) < 9) {
  84. var columnWidth = th.clientWidth
  85. - parseInt($(th).css('paddingLeft'))
  86. - parseInt($(th).css('paddingRight'));
  87. } else { // dimensions from cell itself are not reliable, specially in Opera
  88. var columnWidth = th.getBoundingClientRect().width
  89. - parseInt($(th).css('borderLeftWidth'))
  90. - parseInt($(th).css('paddingLeft'))
  91. - parseInt($(th).css('paddingRight'));
  92. if ($.browser.mozilla) columnWidth -= parseInt($(th).css('borderRightWidth'));
  93. }
  94. $(copyCells.get(n)).css({ 'width': columnWidth, 'minWidth': columnWidth });
  95. });
  96.  
  97. head.css({ 'display': 'block', 'position': 'absolute' });
  98. container.scrollTop(oldPosition);
  99. });
  100.  
  101. $(window).trigger('resize', null);
  102. return container;
  103. }
  104.  
  105. // jQuery plugin
  106. (function ($) {
  107. $.fn.makeScrollable = function (minHeight, maxHeight) {
  108. this.each(function(n, table) {
  109. makeScrollable(table, minHeight, maxHeight);
  110. });
  111. return this;
  112. };
  113. })(jQuery)
Add Comment
Please, Sign In to add comment