bootstrap-table-fixed-columns.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. (function (global, factory) {
  2. if (typeof define === "function" && define.amd) {
  3. define([], factory);
  4. } else if (typeof exports !== "undefined") {
  5. factory();
  6. } else {
  7. var mod = {
  8. exports: {}
  9. };
  10. factory();
  11. global.bootstrapTableFixedColumns = mod.exports;
  12. }
  13. })(this, function () {
  14. 'use strict';
  15. function _classCallCheck(instance, Constructor) {
  16. if (!(instance instanceof Constructor)) {
  17. throw new TypeError("Cannot call a class as a function");
  18. }
  19. }
  20. var _createClass = function () {
  21. function defineProperties(target, props) {
  22. for (var i = 0; i < props.length; i++) {
  23. var descriptor = props[i];
  24. descriptor.enumerable = descriptor.enumerable || false;
  25. descriptor.configurable = true;
  26. if ("value" in descriptor) descriptor.writable = true;
  27. Object.defineProperty(target, descriptor.key, descriptor);
  28. }
  29. }
  30. return function (Constructor, protoProps, staticProps) {
  31. if (protoProps) defineProperties(Constructor.prototype, protoProps);
  32. if (staticProps) defineProperties(Constructor, staticProps);
  33. return Constructor;
  34. };
  35. }();
  36. function _possibleConstructorReturn(self, call) {
  37. if (!self) {
  38. throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
  39. }
  40. return call && (typeof call === "object" || typeof call === "function") ? call : self;
  41. }
  42. var _get = function get(object, property, receiver) {
  43. if (object === null) object = Function.prototype;
  44. var desc = Object.getOwnPropertyDescriptor(object, property);
  45. if (desc === undefined) {
  46. var parent = Object.getPrototypeOf(object);
  47. if (parent === null) {
  48. return undefined;
  49. } else {
  50. return get(parent, property, receiver);
  51. }
  52. } else if ("value" in desc) {
  53. return desc.value;
  54. } else {
  55. var getter = desc.get;
  56. if (getter === undefined) {
  57. return undefined;
  58. }
  59. return getter.call(receiver);
  60. }
  61. };
  62. function _inherits(subClass, superClass) {
  63. if (typeof superClass !== "function" && superClass !== null) {
  64. throw new TypeError("Super expression must either be null or a function, not " + typeof superClass);
  65. }
  66. subClass.prototype = Object.create(superClass && superClass.prototype, {
  67. constructor: {
  68. value: subClass,
  69. enumerable: false,
  70. writable: true,
  71. configurable: true
  72. }
  73. });
  74. if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass;
  75. }
  76. /**
  77. * @author zhixin wen <wenzhixin2010@gmail.com>
  78. */
  79. (function ($) {
  80. $.extend($.fn.bootstrapTable.defaults, {
  81. fixedColumns: false,
  82. fixedNumber: 1
  83. });
  84. $.BootstrapTable = function (_$$BootstrapTable) {
  85. _inherits(_class, _$$BootstrapTable);
  86. function _class() {
  87. _classCallCheck(this, _class);
  88. return _possibleConstructorReturn(this, (_class.__proto__ || Object.getPrototypeOf(_class)).apply(this, arguments));
  89. }
  90. _createClass(_class, [{
  91. key: 'fitHeader',
  92. value: function fitHeader() {
  93. var _get2;
  94. for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
  95. args[_key] = arguments[_key];
  96. }
  97. (_get2 = _get(_class.prototype.__proto__ || Object.getPrototypeOf(_class.prototype), 'fitHeader', this)).call.apply(_get2, [this].concat(args));
  98. if (!this.options.fixedColumns) {
  99. return;
  100. }
  101. if (this.$el.is(':hidden')) {
  102. return;
  103. }
  104. this.$container.find('.fixed-table-header-columns').remove();
  105. this.$fixedHeader = $('<div class="fixed-table-header-columns"></div>');
  106. this.$fixedHeader.append(this.$tableHeader.find('>table').clone(true));
  107. this.$tableHeader.after(this.$fixedHeader);
  108. var width = this.getFixedColumnsWidth();
  109. this.$fixedHeader.css({
  110. top: 0,
  111. width: width,
  112. height: this.$tableHeader.outerHeight(true)
  113. });
  114. this.initFixedColumnsBody();
  115. this.$fixedBody.css({
  116. top: this.$tableHeader.outerHeight(true),
  117. width: width,
  118. height: this.$tableBody.outerHeight(true) - 1
  119. });
  120. this.initFixedColumnsEvents();
  121. }
  122. }, {
  123. key: 'initBody',
  124. value: function initBody() {
  125. var _get3;
  126. for (var _len2 = arguments.length, args = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
  127. args[_key2] = arguments[_key2];
  128. }
  129. (_get3 = _get(_class.prototype.__proto__ || Object.getPrototypeOf(_class.prototype), 'initBody', this)).call.apply(_get3, [this].concat(args));
  130. if (!this.options.fixedColumns) {
  131. return;
  132. }
  133. if (this.options.showHeader && this.options.height) {
  134. return;
  135. }
  136. this.initFixedColumnsBody();
  137. this.$fixedBody.css({
  138. top: 0,
  139. width: this.getFixedColumnsWidth(),
  140. height: this.$tableHeader.outerHeight(true) + this.$tableBody.outerHeight(true)
  141. });
  142. this.initFixedColumnsEvents();
  143. }
  144. }, {
  145. key: 'initFixedColumnsBody',
  146. value: function initFixedColumnsBody() {
  147. this.$container.find('.fixed-table-body-columns').remove();
  148. this.$fixedBody = $('<div class="fixed-table-body-columns"></div>');
  149. this.$fixedBody.append(this.$tableBody.find('>table').clone(true));
  150. this.$tableBody.after(this.$fixedBody);
  151. }
  152. }, {
  153. key: 'getFixedColumnsWidth',
  154. value: function getFixedColumnsWidth() {
  155. var visibleFields = this.getVisibleFields();
  156. var width = 0;
  157. for (var i = 0; i < this.options.fixedNumber; i++) {
  158. width += this.$header.find('th[data-field="' + visibleFields[i] + '"]').outerWidth(true);
  159. }
  160. return width + 1;
  161. }
  162. }, {
  163. key: 'initFixedColumnsEvents',
  164. value: function initFixedColumnsEvents() {
  165. var _this2 = this;
  166. // events
  167. this.$tableBody.off('scroll.fixed-columns').on('scroll.fixed-columns', function (e) {
  168. _this2.$fixedBody.find('table').css('top', -$(e.currentTarget).scrollTop());
  169. });
  170. this.$body.find('> tr[data-index]').off('hover').hover(function (e) {
  171. var index = $(e.currentTarget).data('index');
  172. _this2.$fixedBody.find('tr[data-index="' + index + '"]').css('background-color', $(e.currentTarget).css('background-color'));
  173. }, function (e) {
  174. var index = $(e.currentTarget).data('index');
  175. var $tr = _this2.$fixedBody.find('tr[data-index="' + index + '"]');
  176. $tr.attr('style', $tr.attr('style').replace(/background-color:.*;/, ''));
  177. });
  178. this.$fixedBody.find('tr[data-index]').off('hover').hover(function (e) {
  179. var index = $(e.currentTarget).data('index');
  180. _this2.$body.find('tr[data-index="' + index + '"]').css('background-color', $(e.currentTarget).css('background-color'));
  181. }, function (e) {
  182. var index = $(e.currentTarget).data('index');
  183. var $tr = _this2.$body.find('> tr[data-index="' + index + '"]');
  184. $tr.attr('style', $tr.attr('style').replace(/background-color:.*;/, ''));
  185. });
  186. }
  187. }]);
  188. return _class;
  189. }($.BootstrapTable);
  190. })(jQuery);
  191. });