bootstrap-table-fixed-columns.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /**
  2. * @author zhixin wen <wenzhixin2010@gmail.com>
  3. * @version: v1.0.1
  4. * Modificated 16.08.16 by Aleksej Tokarev (Loecha)
  5. * - Sorting Problem solved
  6. * - Recalculated Size of fixed Columns
  7. */
  8. (function ($) {
  9. 'use strict';
  10. $.extend($.fn.bootstrapTable.defaults, {
  11. fixedColumns: false,
  12. fixedNumber: 1
  13. });
  14. var BootstrapTable = $.fn.bootstrapTable.Constructor,
  15. _initHeader = BootstrapTable.prototype.initHeader,
  16. _initBody = BootstrapTable.prototype.initBody,
  17. _resetView = BootstrapTable.prototype.resetView,
  18. _getCaret = BootstrapTable.prototype.getCaret; // Add: Aleksej
  19. BootstrapTable.prototype.initFixedColumns = function () {
  20. this.$fixedHeader = $([
  21. '<div class="fixed-table-header-columns">',
  22. '<table>',
  23. '<thead></thead>',
  24. '</table>',
  25. '</div>'].join(''));
  26. this.timeoutHeaderColumns_ = 0;
  27. this.$fixedHeader.find('table').attr('class', this.$el.attr('class'));
  28. this.$fixedHeaderColumns = this.$fixedHeader.find('thead');
  29. this.$tableHeader.before(this.$fixedHeader);
  30. this.$fixedBody = $([
  31. '<div class="fixed-table-body-columns">',
  32. '<table>',
  33. '<tbody></tbody>',
  34. '</table>',
  35. '</div>'].join(''));
  36. this.timeoutBodyColumns_ = 0;
  37. this.$fixedBody.find('table').attr('class', this.$el.attr('class'));
  38. this.$fixedBodyColumns = this.$fixedBody.find('tbody');
  39. this.$tableBody.before(this.$fixedBody);
  40. };
  41. BootstrapTable.prototype.initHeader = function () {
  42. _initHeader.apply(this, Array.prototype.slice.apply(arguments));
  43. if (!this.options.fixedColumns) {
  44. return;
  45. }
  46. this.initFixedColumns();
  47. var that = this, $trs = this.$header.find('tr').clone(true); //Fix: Aleksej "clone()" mit "clone(true)" ersetzt
  48. $trs.each(function () {
  49. // This causes layout problems:
  50. //$(this).find('th:gt(' + (that.options.fixedNumber -1) + ')').remove(); // Fix: Aleksej "-1" hinnzugefügt. Denn immer eine Spalte Mehr geblieben ist
  51. $(this).find('th:gt(' + that.options.fixedNumber + ')').remove();
  52. });
  53. this.$fixedHeaderColumns.html('').append($trs);
  54. };
  55. BootstrapTable.prototype.initBody = function () {
  56. _initBody.apply(this, Array.prototype.slice.apply(arguments));
  57. if (!this.options.fixedColumns) {
  58. return;
  59. }
  60. var that = this,
  61. rowspan = 0;
  62. this.$fixedBodyColumns.html('');
  63. this.$body.find('> tr[data-index]').each(function () {
  64. var $tr = $(this).clone(),
  65. $tds = $tr.find('td');
  66. var dataIndex = $tr.attr("data-index");
  67. $tr = $("<tr></tr>");
  68. $tr.attr("data-index", dataIndex);
  69. var end = that.options.fixedNumber;
  70. if (rowspan > 0) {
  71. --end;
  72. --rowspan;
  73. }
  74. for (var i = 0; i < end; i++) {
  75. $tr.append($tds.eq(i).clone());
  76. }
  77. that.$fixedBodyColumns.append($tr);
  78. if ($tds.eq(0).attr('rowspan')){
  79. rowspan = $tds.eq(0).attr('rowspan') - 1;
  80. }
  81. });
  82. };
  83. BootstrapTable.prototype.resetView = function () {
  84. _resetView.apply(this, Array.prototype.slice.apply(arguments));
  85. if (!this.options.fixedColumns) {
  86. return;
  87. }
  88. clearTimeout(this.timeoutHeaderColumns_);
  89. this.timeoutHeaderColumns_ = setTimeout($.proxy(this.fitHeaderColumns, this), this.$el.is(':hidden') ? 100 : 0);
  90. clearTimeout(this.timeoutBodyColumns_);
  91. this.timeoutBodyColumns_ = setTimeout($.proxy(this.fitBodyColumns, this), this.$el.is(':hidden') ? 100 : 0);
  92. };
  93. BootstrapTable.prototype.fitHeaderColumns = function () {
  94. var that = this,
  95. visibleFields = this.getVisibleFields(),
  96. headerWidth = 0;
  97. this.$body.find('tr:first-child:not(.no-records-found) > *').each(function (i) {
  98. var $this = $(this),
  99. index = i;
  100. if (i >= that.options.fixedNumber) {
  101. return false;
  102. }
  103. if (that.options.detailView && !that.options.cardView) {
  104. index = i - 1;
  105. }
  106. var $th = that.$fixedHeader.find('th[data-field="' + visibleFields[index] + '"]');
  107. $th.find('.fht-cell').width($this.innerWidth());
  108. headerWidth += $this.outerWidth();
  109. $th.data('fix-pos', index);
  110. });
  111. this.$fixedHeader.width(headerWidth + 1).show();
  112. // fix click event
  113. this.$fixedHeader.delegate("tr th", 'click', function() {
  114. $(this).parents(".fixed-table-container").find(".fixed-table-body table thead tr th:eq("+$(this).data("fix-pos")+") .sortable").click();
  115. })
  116. };
  117. /**
  118. * Add: Aleksej
  119. * Hook für getCaret. Aktualisieren Header bei Fixed-Columns wenn diese sortiert wurden
  120. * @method getCaret
  121. * @for BootstrapTable
  122. */
  123. BootstrapTable.prototype.getCaret = function () {
  124. var result = _getCaret.apply(this, arguments);
  125. if (this.options.fixedColumns && this.$fixedHeaderColumns instanceof jQuery) {
  126. var that = this, $th;
  127. $.each(this.$fixedHeaderColumns.find('th'), function (i, th) {
  128. $th = $(th);
  129. $th.find('.sortable').removeClass('desc asc').addClass($th.data('field') === that.options.sortName ? that.options.sortOrder : 'both');
  130. });
  131. }
  132. return result;
  133. };
  134. /**
  135. * Add: Aleksej, zum berechnen von Scrollbar-Größe
  136. * @method calcScrollBarSize
  137. * @return Number
  138. */
  139. BootstrapTable.prototype.calcScrollBarSize = function () {
  140. // Es ist egal, ob Höhe oder Breite
  141. var tmpWidth = 100,
  142. $container = $('<div>').css({
  143. width : tmpWidth,
  144. overflow : 'scroll',
  145. visibility : 'hidden'}
  146. ).appendTo('body'),
  147. widthWithScroll = $('<div>').css({
  148. width: '100%'
  149. }).appendTo($container).outerWidth();
  150. $container.remove();
  151. return tmpWidth - widthWithScroll;
  152. };
  153. BootstrapTable.prototype.fitBodyColumns = function () {
  154. var that = this,
  155. borderHeight = (parseInt(this.$el.css('border-bottom-width')) + parseInt(this.$el.css('border-top-width'))), // Add. Aleksej
  156. top = this.$fixedHeader.outerHeight() + borderHeight, // Fix. Aleksej "-2" mit "+ borderHeight" ersetzt
  157. // the fixed height should reduce the scorll-x height
  158. height = this.$tableBody.height() - this.calcScrollBarSize(); // Fix. Aleksej "-14" mit "- this.calcScrollBarSize()" ersetzt
  159. if (!this.$body.find('> tr[data-index]').length) {
  160. this.$fixedBody.hide();
  161. return;
  162. }
  163. if (!this.options.height) {
  164. top = this.$fixedHeader.height();
  165. height = height - top;
  166. }
  167. this.$fixedBody.css({
  168. width: this.$fixedHeader.width(),
  169. height: height,
  170. top: top
  171. }).show();
  172. this.$body.find('> tr').each(function (i) {
  173. that.$fixedBody.find('tr:eq(' + i + ')').height($(this).height() - 1);
  174. });
  175. // events
  176. this.$tableBody.on('scroll', function () {
  177. that.$fixedBody.find('table').css('top', -$(this).scrollTop());
  178. });
  179. this.$body.find('> tr[data-index]').off('hover').hover(function () {
  180. var index = $(this).data('index');
  181. that.$fixedBody.find('tr[data-index="' + index + '"]').addClass('hover');
  182. }, function () {
  183. var index = $(this).data('index');
  184. that.$fixedBody.find('tr[data-index="' + index + '"]').removeClass('hover');
  185. });
  186. this.$fixedBody.find('tr[data-index]').off('hover').hover(function () {
  187. var index = $(this).data('index');
  188. that.$body.find('tr[data-index="' + index + '"]').addClass('hover');
  189. }, function () {
  190. var index = $(this).data('index');
  191. that.$body.find('> tr[data-index="' + index + '"]').removeClass('hover');
  192. });
  193. // fix td width bug
  194. var $first_tr = that.$body.find('tr:eq(0)');
  195. that.$fixedBody.find('tr:eq(0)').find("td").each(function(index) {
  196. $(this).width($first_tr.find("td:eq("+index+")").width())
  197. });
  198. };
  199. })(jQuery);