bootstrap-table-sticky-header.js 5.4 KB

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