bootstrap-table-sticky-header.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /**
  2. * @author vincent loh <vincent.ml@gmail.com>
  3. * @version: v1.0.0
  4. * https://github.com/vinzloh/bootstrap-table/
  5. * Sticky header for bootstrap-table
  6. */
  7. (function ($) {
  8. 'use strict';
  9. var sprintf = $.fn.bootstrapTable.utils.sprintf;
  10. $.extend($.fn.bootstrapTable.defaults, {
  11. stickyHeader: false
  12. });
  13. var BootstrapTable = $.fn.bootstrapTable.Constructor,
  14. _initHeader = BootstrapTable.prototype.initHeader;
  15. BootstrapTable.prototype.initHeader = function () {
  16. var that = this;
  17. _initHeader.apply(this, Array.prototype.slice.apply(arguments));
  18. if (!this.options.stickyHeader) return;
  19. var table = this.$tableBody.find('table');
  20. var table_id = table.attr('id');
  21. var header_id = table.attr('id') + '-sticky-header';
  22. var sticky_header_container_id = header_id +'-sticky-header-container';
  23. var anchor_begin_id = header_id +'_sticky_anchor_begin';
  24. var anchor_end_id = header_id +'_sticky_anchor_end';
  25. // add begin and end anchors to track table position
  26. table.before(sprintf('<div id="%s" class="hidden"></div>', sticky_header_container_id));
  27. table.before(sprintf('<div id="%s"></div>', anchor_begin_id));
  28. table.after(sprintf('<div id="%s"></div>', anchor_end_id));
  29. table.find('thead').attr('id', header_id);
  30. // clone header just once, to be used as sticky header
  31. // deep clone header. using source header affects tbody>td width
  32. this.$stickyHeader = $($('#'+header_id).clone());
  33. // avoid id conflict
  34. this.$stickyHeader.removeAttr('id');
  35. // render sticky on window scroll or resize
  36. $(window).on('resize.'+table_id, table, render_sticky_header);
  37. $(window).on('scroll.'+table_id, table, render_sticky_header);
  38. // render sticky when table scroll left-right
  39. table.closest('.fixed-table-container').find('.fixed-table-body').on('scroll.'+table_id, table, match_position_x);
  40. function render_sticky_header(event){
  41. var table = event.data;
  42. var table_header_id = table.find('thead').attr('id');
  43. // console.log('render_sticky_header for > '+table_header_id);
  44. if (table.length < 1 || $('#'+table_id).length < 1){
  45. // turn off window listeners
  46. $(window).off('resize.'+table_id);
  47. $(window).off('scroll.'+table_id);
  48. table.closest('.fixed-table-container').find('.fixed-table-body').off('scroll.'+table_id);
  49. return;
  50. }
  51. // get header height
  52. var header_height = '0';
  53. if (that.options.stickyHeaderOffsetY) header_height = that.options.stickyHeaderOffsetY.replace('px','');
  54. // window scroll top
  55. var t = $(window).scrollTop();
  56. // top anchor scroll position, minus header height
  57. var e = $("#"+anchor_begin_id).offset().top - header_height;
  58. // bottom anchor scroll position, minus header height, minus sticky height
  59. var e_end = $("#"+anchor_end_id).offset().top - header_height - $('#'+table_header_id).css('height').replace('px','');
  60. // show sticky when top anchor touches header, and when bottom anchor not exceeded
  61. if (t > e && t <= e_end) {
  62. // ensure clone and source column widths are the same
  63. $.each( that.$stickyHeader.find('tr').eq(0).find('th'), function (index, item) {
  64. $(item).css('min-width', $('#'+table_header_id+' tr').eq(0).find('th').eq(index).css('width'));
  65. });
  66. // match bootstrap table style
  67. $("#"+sticky_header_container_id).removeClass('hidden').addClass("fix-sticky fixed-table-container") ;
  68. // stick it in position
  69. $("#"+sticky_header_container_id).css('top', header_height + 'px');
  70. // create scrollable container for header
  71. var scrollable_div = $('<div style="position:absolute;width:100%;overflow-x:hidden;" />');
  72. // append cloned header to dom
  73. $("#"+sticky_header_container_id).html(scrollable_div.append(that.$stickyHeader));
  74. // match clone and source header positions when left-right scroll
  75. match_position_x(event);
  76. } else {
  77. // hide sticky
  78. $("#"+sticky_header_container_id).removeClass("fix-sticky").addClass('hidden');
  79. }
  80. }
  81. function match_position_x(event){
  82. var table = event.data;
  83. var table_header_id = table.find('thead').attr('id');
  84. // match clone and source header positions when left-right scroll
  85. $("#"+sticky_header_container_id).css(
  86. 'width', +table.closest('.fixed-table-body').css('width').replace('px', '') + 1
  87. );
  88. $("#"+sticky_header_container_id+" thead").parent().scrollLeft(Math.abs($('#'+table_header_id).position().left));
  89. }
  90. };
  91. })(jQuery);