[trick] Freeze Header Table Dengan plugin Javascript

Bismillah…

Langsung aja gw mau kasih trick, trick yang umum udah buanyak di google, gw lupa source nyah, ini sih sekedar pengingat gw ajah… ini script js nyah:

(function($) {

$.fn.fixedHeader = function (options) {
 var config = {
   topOffset: 90,
   bgColor: 'white'
 };
 if (options){ $.extend(config, options); }

 return this.each( function() {
  var o = $(this);

  var $win = $(window)
    , $head = $('thead.header', o)
    , isFixed = 0;
  var headTop = $head.length && $head.offset().top - config.topOffset;

  function processScroll() {
    if (!o.is(':visible')) return;
    if ($('thead.header-copy').size()) {
      $('thead.header-copy').width($head.width());
      var i, scrollTop = $win.scrollTop();
    }
    var t = $head.length && $head.offset().top - config.topOffset;
    if (!isFixed && headTop != t) { headTop = t; }
    if (scrollTop >= headTop && !isFixed) {
      isFixed = 1;
    } else if (scrollTop <= headTop && isFixed) { isFixed = 0; } isFixed ? $('thead.header-copy', o).show().offset({ left: $head.offset().left }) : $('thead.header-copy', o).hide(); } $win.on('scroll', processScroll); // hack sad times - holdover until rewrite for 2.1 $head.on('click', function () { if (!isFixed) setTimeout(function () { $win.scrollTop($win.scrollTop() - 47) }, 10); }) $head.clone(true).removeClass('header').addClass('header-copy header-fixed').css({'position': 'fixed', 'top': config['topOffset']}).appendTo(o); o.find('thead.header-copy').width($head.width()); o.find('thead.header > tr > th').each(function (i, h) {
    var w = $(h).width();
    o.find('thead.header-copy> tr > th:eq('+i+')').width(w)
  });
  $head.css({ margin:'0 auto',
              width: o.width(),
             'background-color':config.bgColor });
  processScroll();
 });
};

})(jQuery);

Kalo males, liat aja disini: link

Terus cara pakenya gimana? tenang ini script js nyah:

$(document).ready(function(){
    $('.table-fixed-header').fixedHeader();
});

Nah sintak htmlnya jangan lupa, misalkan:

<table class="table-fixed-header">
<thead class="header">

</thead>
</table>

ini buat css nyah:

  thead {
    background-color: #eaeaea;
}

Done! simple lah… silahkan manfaatkan!

Have a nice day…

Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: