javascript - HTML table with fixed headers? -


is there cross-browser css/javascript technique display long html table such column headers stay fixed on-screen , not scroll table body. think of "freeze panes" effect in microsoft excel.

i want able scroll through contents of table, able see column headers @ top.

i looking solution while , found of answers not working or not suitable situation, wrote simple solution jquery.

this solution outline.

  1. clone table needs have fixed header , place cloned copy on top of original
  2. remove table body top table
  3. remove table header bottom table
  4. adjust column widths. (we remembering original column widths)

below code. here's demo fixed header demo

<head>     <script       src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js">     </script>     <script>      function scrolify(tblasjqueryobject, height){         var otbl = tblasjqueryobject;          // large tables can remove 4 lines below         // , wrap table <div> in mark-up , assign         // height , overflow property           var otbldiv = $("<div/>");         otbldiv.css('height', height);         otbldiv.css('overflow','scroll');                        otbl.wrap(otbldiv);          // save original width         otbl.attr("data-item-original-width", otbl.width());         otbl.find('thead tr td').each(function(){             $(this).attr("data-item-original-width",$(this).width());         });          otbl.find('tbody tr:eq(0) td').each(function(){             $(this).attr("data-item-original-width",$(this).width());         });                            // clone original table         var newtbl = otbl.clone();          // remove table header original table         otbl.find('thead tr').remove();                          // remove table body new table         newtbl.find('tbody tr').remove();             otbl.parent().parent().prepend(newtbl);         newtbl.wrap("<div/>");          // replace original column width                         newtbl.width(newtbl.attr('data-item-original-width'));         newtbl.find('thead tr td').each(function(){             $(this).width($(this).attr("data-item-original-width"));         });              otbl.width(otbl.attr('data-item-original-width'));               otbl.find('tbody tr:eq(0) td').each(function(){             $(this).width($(this).attr("data-item-original-width"));         });                      }      $(document).ready(function(){         scrolify($('#tblneedsscrolling'), 160); // 160 height     });       </script>   </head>  <body>     <div style="width:300px;border:6px green solid;">         <table border="1" width="100%" id="tblneedsscrolling">             <thead>                 <tr><th>header 1</th><th>header 2</th></tr>             </thead>             <tbody>                 <tr><td>row 1, cell 1</td><td>row 1, cell 2</td></tr>                 <tr><td>row 2, cell 1</td><td>row 2, cell 2</td></tr>                 <tr><td>row 3, cell 1</td><td>row 3, cell 2</td></tr>                 <tr><td>row 4, cell 1</td><td>row 4, cell 2</td></tr>                            <tr><td>row 5, cell 1</td><td>row 5, cell 2</td></tr>                 <tr><td>row 6, cell 1</td><td>row 6, cell 2</td></tr>                 <tr><td>row 7, cell 1</td><td>row 7, cell 2</td></tr>                 <tr><td>row 8, cell 1</td><td>row 8, cell 2</td></tr>                        </tbody>         </table>     </div>  </body> 

this solution works in chrome & ie. since based on jquery should work in other jquery supported browsers well.


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -