scroll-grid.txt 6.5 KB

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