Нет описания

bootstrap-table-fixed-columns.js 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /**
  2. * @author zhixin wen <wenzhixin2010@gmail.com>
  3. * @version: v1.0.1
  4. */
  5. (function ($) {
  6. 'use strict';
  7. $.extend($.fn.bootstrapTable.defaults, {
  8. fixedColumns: false,
  9. fixedNumber: 1
  10. });
  11. var BootstrapTable = $.fn.bootstrapTable.Constructor,
  12. _initHeader = BootstrapTable.prototype.initHeader,
  13. _initBody = BootstrapTable.prototype.initBody,
  14. _resetView = BootstrapTable.prototype.resetView;
  15. BootstrapTable.prototype.initFixedColumns = function () {
  16. this.$fixedHeader = $([
  17. '<div class="fixed-table-header-columns">',
  18. '<table>',
  19. '<thead></thead>',
  20. '</table>',
  21. '</div>'].join(''));
  22. this.timeoutHeaderColumns_ = 0;
  23. this.$fixedHeader.find('table').attr('class', this.$el.attr('class'));
  24. this.$fixedHeaderColumns = this.$fixedHeader.find('thead');
  25. this.$tableHeader.before(this.$fixedHeader);
  26. this.$fixedBody = $([
  27. '<div class="fixed-table-body-columns">',
  28. '<table>',
  29. '<tbody></tbody>',
  30. '</table>',
  31. '</div>'].join(''));
  32. this.timeoutBodyColumns_ = 0;
  33. this.$fixedBody.find('table').attr('class', this.$el.attr('class'));
  34. this.$fixedBodyColumns = this.$fixedBody.find('tbody');
  35. this.$tableBody.before(this.$fixedBody);
  36. };
  37. BootstrapTable.prototype.initHeader = function () {
  38. _initHeader.apply(this, Array.prototype.slice.apply(arguments));
  39. if (!this.options.fixedColumns) {
  40. return;
  41. }
  42. this.initFixedColumns();
  43. var that = this, $trs = this.$header.find('tr').clone();
  44. $trs.each(function () {
  45. $(this).find('th:gt(' + that.options.fixedNumber + ')').remove();
  46. });
  47. this.$fixedHeaderColumns.html('').append($trs);
  48. };
  49. BootstrapTable.prototype.initBody = function () {
  50. _initBody.apply(this, Array.prototype.slice.apply(arguments));
  51. if (!this.options.fixedColumns) {
  52. return;
  53. }
  54. var that = this,
  55. rowspan = 0;
  56. this.$fixedBodyColumns.html('');
  57. this.$body.find('> tr[data-index]').each(function () {
  58. var $tr = $(this).clone(),
  59. $tds = $tr.find('td');
  60. $tr.html('');
  61. var end = that.options.fixedNumber;
  62. if (rowspan > 0) {
  63. --end;
  64. --rowspan;
  65. }
  66. for (var i = 0; i < end; i++) {
  67. $tr.append($tds.eq(i).clone());
  68. }
  69. that.$fixedBodyColumns.append($tr);
  70. if ($tds.eq(0).attr('rowspan')){
  71. rowspan = $tds.eq(0).attr('rowspan') - 1;
  72. }
  73. });
  74. };
  75. BootstrapTable.prototype.resetView = function () {
  76. _resetView.apply(this, Array.prototype.slice.apply(arguments));
  77. if (!this.options.fixedColumns) {
  78. return;
  79. }
  80. clearTimeout(this.timeoutHeaderColumns_);
  81. this.timeoutHeaderColumns_ = setTimeout($.proxy(this.fitHeaderColumns, this), this.$el.is(':hidden') ? 100 : 0);
  82. clearTimeout(this.timeoutBodyColumns_);
  83. this.timeoutBodyColumns_ = setTimeout($.proxy(this.fitBodyColumns, this), this.$el.is(':hidden') ? 100 : 0);
  84. };
  85. BootstrapTable.prototype.fitHeaderColumns = function () {
  86. var that = this,
  87. visibleFields = this.getVisibleFields(),
  88. headerWidth = 0;
  89. this.$body.find('tr:first-child:not(.no-records-found) > *').each(function (i) {
  90. var $this = $(this),
  91. index = i;
  92. if (i >= that.options.fixedNumber) {
  93. return false;
  94. }
  95. if (that.options.detailView && !that.options.cardView) {
  96. index = i - 1;
  97. }
  98. that.$fixedHeader.find('th[data-field="' + visibleFields[index] + '"]')
  99. .find('.fht-cell').width($this.innerWidth());
  100. headerWidth += $this.outerWidth();
  101. });
  102. this.$fixedHeader.width(headerWidth + 1).show();
  103. };
  104. BootstrapTable.prototype.fitBodyColumns = function () {
  105. var that = this,
  106. top = -(parseInt(this.$el.css('margin-top')) - 2),
  107. // the fixed height should reduce the scorll-x height
  108. height = this.$tableBody.height() - 14;
  109. if (!this.$body.find('> tr[data-index]').length) {
  110. this.$fixedBody.hide();
  111. return;
  112. }
  113. if (!this.options.height) {
  114. top = this.$fixedHeader.height();
  115. height = height - top;
  116. }
  117. this.$fixedBody.css({
  118. width: this.$fixedHeader.width(),
  119. height: height,
  120. top: top
  121. }).show();
  122. this.$body.find('> tr').each(function (i) {
  123. that.$fixedBody.find('tr:eq(' + i + ')').height($(this).height() - 1);
  124. });
  125. // events
  126. this.$tableBody.on('scroll', function () {
  127. that.$fixedBody.find('table').css('top', -$(this).scrollTop());
  128. });
  129. this.$body.find('> tr[data-index]').off('hover').hover(function () {
  130. var index = $(this).data('index');
  131. that.$fixedBody.find('tr[data-index="' + index + '"]').addClass('hover');
  132. }, function () {
  133. var index = $(this).data('index');
  134. that.$fixedBody.find('tr[data-index="' + index + '"]').removeClass('hover');
  135. });
  136. this.$fixedBody.find('tr[data-index]').off('hover').hover(function () {
  137. var index = $(this).data('index');
  138. that.$body.find('tr[data-index="' + index + '"]').addClass('hover');
  139. }, function () {
  140. var index = $(this).data('index');
  141. that.$body.find('> tr[data-index="' + index + '"]').removeClass('hover');
  142. });
  143. };
  144. })(jQuery);