Fixed table header row and column in jQuery


SUBMITTED BY: Guest

DATE: Jan. 14, 2014, 7:21 p.m.

FORMAT: JavaScript

SIZE: 6.7 kB

HITS: 17781

  1. <html>
  2. <!--
  3. copyright pavan
  4. http://fixed-header-using-jquery.blogspot.com/2009/05/scrollable-table-with-fixed-header-and.html
  5. -->
  6. <head>
  7. <script type="text/javascript" src="http://jqueryui.com/latest/jquery-1.3.2.js"></script>
  8. <script>
  9. $(document).ready(function(){
  10. fnAdjustTable();
  11. });
  12. fnAdjustTable = function(){
  13. var colCount = $('#firstTr>td').length; //get total number of column
  14. var m = 0;
  15. var n = 0;
  16. var brow = 'mozilla';
  17. jQuery.each(jQuery.browser, function(i, val) {
  18. if(val == true){
  19. brow = i.toString();
  20. }
  21. });
  22. $('.tableHeader').each(function(i){
  23. if (m < colCount){
  24. if (brow == 'mozilla'){
  25. $('#firstTd').css("width",$('.tableFirstCol').innerWidth());//for adjusting first td
  26. $(this).css('width',$('#table_div td:eq('+m+')').innerWidth());//for assigning width to table Header div
  27. }
  28. else if (brow == 'msie'){
  29. $('#firstTd').css("width",$('.tableFirstCol').width());
  30. $(this).css('width',$('#table_div td:eq('+m+')').width()-2);//In IE there is difference of 2 px
  31. }
  32. else if (brow == 'safari'){
  33. $('#firstTd').css("width",$('.tableFirstCol').width());
  34. $(this).css('width',$('#table_div td:eq('+m+')').width());
  35. }
  36. else {
  37. $('#firstTd').css("width",$('.tableFirstCol').width());
  38. $(this).css('width',$('#table_div td:eq('+m+')').innerWidth());
  39. }
  40. }
  41. m++;
  42. });
  43. $('.tableFirstCol').each(function(i){
  44. if(brow == 'mozilla'){
  45. $(this).css('height',$('#table_div td:eq('+colCount*n+')').outerHeight());//for providing height using scrollable table column height
  46. }
  47. else if(brow == 'msie'){
  48. $(this).css('height',$('#table_div td:eq('+colCount*n+')').innerHeight()-2);
  49. }
  50. else {
  51. $(this).css('height',$('#table_div td:eq('+colCount*n+')').height());
  52. }
  53. n++;
  54. });
  55. }
  56. //function to support scrolling of title and first column
  57. fnScroll = function(){
  58. $('#divHeader').scrollLeft($('#table_div').scrollLeft());
  59. $('#firstcol').scrollTop($('#table_div').scrollTop());
  60. }
  61. </script>
  62. </head>
  63. <body>
  64. <table cellspacing="0" cellpadding="0" border="0" >
  65. <tr>
  66. <td id="firstTd">
  67. </td>
  68. <td>
  69. <div id="divHeader" style="overflow:hidden;width:284px;">
  70. <table cellspacing="0" cellpadding="0" border="1" >
  71. <tr>
  72. <td>
  73. <div class="tableHeader">Title1</div>
  74. </td>
  75. <td>
  76. <div class="tableHeader">Title2</div>
  77. </td>
  78. <td>
  79. <div class="tableHeader">Title3</div>
  80. </td>
  81. <td>
  82. <div class="tableHeader">Title4</div>
  83. </td>
  84. <td>
  85. <div class="tableHeader">Title5</div>
  86. </td>
  87. </tr>
  88. </table>
  89. </div>
  90. </td>
  91. </tr>
  92. <tr>
  93. <td valign="top">
  94. <div id="firstcol" style="overflow: hidden;height:80px">
  95. <table width="200px" cellspacing="0" cellpadding="0" border="1" >
  96. <tr>
  97. <td class="tableFirstCol">First Col row1 </td>
  98. </tr>
  99. <tr>
  100. <td class="tableFirstCol">First Col row2</td>
  101. </tr>
  102. <tr>
  103. <td class="tableFirstCol">First Col row3</td>
  104. </tr>
  105. <tr>
  106. <td class="tableFirstCol">First Col row4</td>
  107. </tr>
  108. <tr>
  109. <td class="tableFirstCol">First Col row5</td>
  110. </tr>
  111. <tr>
  112. <td class="tableFirstCol">First Col row6</td>
  113. </tr>
  114. <tr>
  115. <td class="tableFirstCol">First Col row7</td>
  116. </tr>
  117. <tr>
  118. <td class="tableFirstCol">First Col row8</td>
  119. </tr>
  120. <tr>
  121. <td class="tableFirstCol">First Col row9</td>
  122. </tr>
  123. </table>
  124. </div>
  125. </td>
  126. <td valign="top">
  127. <div id="table_div" style="overflow: scroll;width:300px;height:100px;position:relative" onscroll="fnScroll()" >
  128. <table width="500px" cellspacing="0" cellpadding="0" border="1" >
  129. <tr id='firstTr'>
  130. <td>Row1Col1</td>
  131. <td>Row1Col2</td>
  132. <td>Row1Col3</td>
  133. <td>Row1Col4</td>
  134. <td>Row1Col5</td>
  135. </tr>
  136. <tr>
  137. <td>Row2Col1</td>
  138. <td>Row2Col2</td>
  139. <td>Row2Col3</td>
  140. <td>Row2Col4</td>
  141. <td>Row3Col5</td>
  142. </tr>
  143. <tr>
  144. <td>Row3Col1</td>
  145. <td>Row3Col2</td>
  146. <td>Row3Col3</td>
  147. <td>Row3Col4</td>
  148. <td>Row3Col5</td>
  149. </tr>
  150. <tr>
  151. <td>Row4Col1</td>
  152. <td>Row4Col2</td>
  153. <td>Row4Col3</td>
  154. <td>Row4Col4</td>
  155. <td>Row4Col5</td>
  156. </tr>
  157. <tr>
  158. <td>Row5Col1</td>
  159. <td>Row5Col2</td>
  160. <td>Row5Col3</td>
  161. <td>Row5Col4</td>
  162. <td>Row5Col5</td>
  163. </tr>
  164. <tr>
  165. <td>Row6Col1</td>
  166. <td>Row6Col2</td>
  167. <td>Row6Col3 is both wider and<br />taller than surrounding cells, yet<br />fixed elements still line up correctly</td>
  168. <td>Row6Col4</td>
  169. <td>Row6Col5</td>
  170. </tr>
  171. <tr>
  172. <td>Row7Col1</td>
  173. <td>Row7Col2</td>
  174. <td>Row7Col3</td>
  175. <td>Row7Col4</td>
  176. <td>Row7Col5</td>
  177. </tr>
  178. <tr>
  179. <td>Row8Col1</td>
  180. <td>Row8Col2</td>
  181. <td>Row8Col3</td>
  182. <td>Row8Col4</td>
  183. <td>Row8Col5</td>
  184. </tr>
  185. <tr>
  186. <td>Row9Col1</td>
  187. <td>Row9Col2</td>
  188. <td>Row9Col3</td>
  189. <td>Row9Col4</td>
  190. <td>Row9Col5</td>
  191. </tr>
  192. </table>
  193. </div>
  194. </td>
  195. </tr>
  196. </table>
  197. </body>
  198. </html>

comments powered by Disqus