How to create table with fixed header using css and jquery | Sanwebcorner

How to create table with fixed header using css and jquery

This is one of the simple concept to fix the table header when scroll. This method is done by with the help of css and jquery. This type of fixed header helps the customers to view the data with header perfectly. Mostly this will used for large amount of data because when customer scrolling down they may forgot the particular field is related to which header so to solve this type of problems we should use the fixed header for table.

The example below shows the table with fixed header also available the demo page for live demo of this program. I hope this will very helpful.


Html code for Fixed header Table

<!DOCTYPE html>
<html >
<head>
  <meta charset="UTF-8">
  <title>How to create Table with fixed header on scroll</title>
  <meta name="viewport" content="initial-scale=1.0">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
   <script src="js/fixedheader.js"></script>
      <link rel="stylesheet" href="css/my-style.css">
</head>
<body>
<table class="pink" align="center">
  <thead>
    <tr>
      <th>Student Name</th>
      <th>Age</th>
      <th>Salary</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Student1</td>
      <td>28</td>
      <td>8000</td>
    </tr>
    <tr>
      <td>Student2</td>
      <td>28</td>
      <td>8000</td>
    </tr>
    <tr>
      <td>Student3</td>
      <td>28</td>
      <td>8000</td>
    </tr>
    <tr>
      <td>Student4</td>
      <td>28</td>
      <td>8000</td>
    </tr>
    <tr>
      <td>Student5</td>
      <td>28</td>
      <td>8000</td>
    </tr>
    <tr>
      <td>Student6</td>
      <td>28</td>
      <td>8000</td>
    </tr>
    <tr>
      <td>Student7</td>
      <td>28</td>
      <td>8000</td>
    </tr>
    <tr>
      <td>Student8</td>
      <td>28</td>
      <td>8000</td>
    </tr>
    <tr>
     <td>Student9</td>
      <td>28</td>
      <td>8000</td>
    </tr>
    <tr>
      <td>Student10</td>
      <td>28</td>
      <td>8000</td>
    </tr>
    <tr>
      <td>Student11</td>
      <td>28</td>
      <td>8000</td>
    </tr>
    <tr>
      <td>Student12</td>
      <td>28</td>
      <td>8000</td>
    </tr>
    <tr>
      <td>Student13</td>
      <td>28</td>
      <td>8000</td>
    </tr>
    <tr>
      <td>Student14</td>
      <td>28</td>
      <td>8000</td>
    </tr>
    <tr>
      <td>Student15</td>
      <td>28</td>
      <td>8000</td>
    </tr>
    <tr>
      <td>Student16</td>
      <td>28</td>
      <td>8000</td>
    </tr>
    <tr>
      <td>Student17</td>
      <td>28</td>
      <td>8000</td>
    </tr>
    <tr>
      <td>Student18</td>
      <td>28</td>
      <td>8000</td>
    </tr>
    <tr>
      <td>Student19</td>
      <td>28</td>
      <td>8000</td>
    </tr>
       <tr>
      <td>Student20</td>
      <td>28</td>
      <td>8000</td>
    </tr>
  </tbody>
</table>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<a href="http://www.sanwebcorner.com/">www.sanwebcorner.com</a>
</body>
</html>

Css  for Fixed header Table

.container
{
width:80%;
}
table{
  border-collapse:collapse;
  width:100%;
}
.pink{
  border:1px solid #E91E63;
}

.pink thead{
  background:#E91E63;
}

thead{
  color:white;
}

th,td{
  text-align:center;
  padding:5px 0;
}

tbody tr:nth-child(even){
  background:#ECF0F1;
}

tbody tr:hover{
background:#BDC3C7;
  color:#FFFFFF;
}

.fixed{
  top:0;
  position:fixed;
  width:auto;
  display:none;
  border:none;
}

Javascript  for Fixed header Table

;(function($) {
   $.fn.fixMe = function() {
      return this.each(function() {
         var $this = $(this),
            $t_fixed;
         function init() {
            $this.wrap('');
            $t_fixed = $this.clone();
            $t_fixed.find("tbody").remove().end().addClass("fixed").insertBefore($this);
            resizeFixed();
         }
         function resizeFixed() {
            $t_fixed.find("th").each(function(index) {
               $(this).css("width",$this.find("th").eq(index).outerWidth()+"px");
            });
         }
         function scrollFixed() {
            var offset = $(this).scrollTop(),
            tableOffsetTop = $this.offset().top,
            tableOffsetBottom = tableOffsetTop + $this.height() - $this.find("thead").height();
            if(offset < tableOffsetTop || offset > tableOffsetBottom)
               $t_fixed.hide();
            else if(offset >= tableOffsetTop && offset <= tableOffsetBottom && $t_fixed.is(":hidden"))
               $t_fixed.show();
         }
         $(window).resize(resizeFixed);
         $(window).scroll(scrollFixed);
         init();
      });
   };
})(jQuery);

$(document).ready(function(){
   $("table").fixMe();
   $(".up").click(function() {
      $('html, body').animate({
      scrollTop: 0
   }, 2000);
 });
});

Post a Comment

Labels

360 degree view product image 3d rotate 3d text 3step-form Accordion menu ajax All Browsers Animated Background Gradient animation audio controller auto complete Best accordion menu bootstrap border bounce effect on hover Breadcrumb calculate age by dob calendar Captcha change image on mouse move color chooser countdown Counter on scroll create random number Css css animation css buttons css hover css3 customized upload Button Date picker delete data from table Different scroll speed email otp verification email verifier Embed eventlist Expanding Menu feedbck form flash news section close button full screen menu Google custom search Google Maps Google reCAPTCHA Google site search gradient hide menu Horizontal News ticker Hover Htm Html html dropdown html form html5 html5 form validation Iframe image hover increment decrement textbox value based on select box Javascript Jquery js Layered image effect on mouse hover lock & unlock page scroll marquee marquee with close button mobile number validation mobile otp login Multi-level-drop-down-menu multi-level-form Multi-Step-form mysql News ticker on hover change text to image our team page password generator password strength checker Photoshop PHP populate html selectbox Popup image on click the link pure css Question type form random number generate Random password Redirect page Based on the select box while submit the form responsive responsive menu responsive popup box Responsive Vertical Time line design rotate image selectbox based on select box simple coming soon page simple countdown based on end date simple typing effect slider sliding tab sms otp login statistic counter on scroll switch tag shape team member Time picker toggle toggle view transparent button underline animation up down slide validation vertical fixed sidebar Webtips Youtube