Bez popisu

bootstrap-table-fixed-columns.js 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. (function ($) {
  2. 'use strict';
  3. $.extend($.fn.bootstrapTable.defaults, {
  4. fixedColumns: false,
  5. fixedNumber: 1
  6. });
  7. var BootstrapTable = $.fn.bootstrapTable.Constructor,
  8. _initHeader = BootstrapTable.prototype.initHeader,
  9. _initBody = BootstrapTable.prototype.initBody,
  10. _resetView = BootstrapTable.prototype.resetView;
  11. BootstrapTable.prototype.initFixedColumns = function () {
  12. this.$fixedBody = $([
  13. '<div class="fixed-table-column" style="position: absolute; background-color: #fff; border-right:1px solid #ddd;">',
  14. '<table>',
  15. '<thead></thead>',
  16. '<tbody></tbody>',
  17. '</table>',
  18. '</div>'].join(''));
  19. this.timeoutHeaderColumns_ = 0;
  20. this.timeoutBodyColumns_ = 0;
  21. this.$fixedBody.find('table').attr('class', this.$el.attr('class'));
  22. this.$fixedHeaderColumns = this.$fixedBody.find('thead');
  23. this.$fixedBodyColumns = this.$fixedBody.find('tbody');
  24. this.$tableBody.before(this.$fixedBody);
  25. };
  26. BootstrapTable.prototype.initHeader = function () {
  27. _initHeader.apply(this, Array.prototype.slice.apply(arguments));
  28. if (!this.options.fixedColumns) {
  29. return;
  30. }
  31. this.initFixedColumns();
  32. var $tr = this.$header.find('tr:eq(0)').clone(),
  33. $ths = $tr.clone().find('th');
  34. $tr.html('');
  35. for (var i = 0; i < this.options.fixedNumber; i++) {
  36. $tr.append($ths.eq(i).clone());
  37. }
  38. this.$fixedHeaderColumns.html('').append($tr);
  39. };
  40. BootstrapTable.prototype.initBody = function () {
  41. _initBody.apply(this, Array.prototype.slice.apply(arguments));
  42. if (!this.options.fixedColumns) {
  43. return;
  44. }
  45. var that = this;
  46. this.$fixedBodyColumns.html('');
  47. this.$body.find('> tr[data-index]').each(function () {
  48. var $tr = $(this).clone(),
  49. $tds = $tr.clone().find('td');
  50. $tr.html('');
  51. for (var i = 0; i < that.options.fixedNumber; i++) {
  52. $tr.append($tds.eq(i).clone());
  53. }
  54. that.$fixedBodyColumns.append($tr);
  55. });
  56. };
  57. BootstrapTable.prototype.resetView = function () {
  58. _resetView.apply(this, Array.prototype.slice.apply(arguments));
  59. if (!this.options.fixedColumns) {
  60. return;
  61. }
  62. clearTimeout(this.timeoutHeaderColumns_);
  63. this.timeoutHeaderColumns_ = setTimeout($.proxy(this.fitHeaderColumns, this), this.$el.is(':hidden') ? 100 : 0);
  64. clearTimeout(this.timeoutBodyColumns_);
  65. this.timeoutBodyColumns_ = setTimeout($.proxy(this.fitBodyColumns, this), this.$el.is(':hidden') ? 100 : 0);
  66. };
  67. BootstrapTable.prototype.fitHeaderColumns = function () {
  68. var that = this,
  69. visibleFields = this.getVisibleFields(),
  70. headerWidth = 0;
  71. this.$body.find('tr:first-child:not(.no-records-found) > *').each(function (i) {
  72. var $this = $(this),
  73. index = i;
  74. if (i >= that.options.fixedNumber) {
  75. return false;
  76. }
  77. if (that.options.detailView && !that.options.cardView) {
  78. index = i - 1;
  79. }
  80. that.$fixedBody.find('thead th[data-field="' + visibleFields[index] + '"]')
  81. .find('.fht-cell').width($this.innerWidth() - 1);
  82. headerWidth += $this.outerWidth();
  83. });
  84. this.$fixedBody.width(headerWidth - 1).show();
  85. };
  86. BootstrapTable.prototype.fitBodyColumns = function () {
  87. var that = this,
  88. top = -(parseInt(this.$el.css('margin-top')) - 2),
  89. height = this.$tableBody.height() - 2;
  90. if (!this.$body.find('> tr[data-index]').length) {
  91. this.$fixedBody.hide();
  92. return;
  93. }
  94. this.$body.find('> tr').each(function (i) {
  95. that.$fixedBody.find('tbody tr:eq(' + i + ')').height($(this).height() - 1);
  96. });
  97. //// events
  98. this.$tableBody.on('scroll', function () {
  99. that.$fixedBody.find('table').css('top', -$(this).scrollTop());
  100. });
  101. this.$body.find('> tr[data-index]').off('hover').hover(function () {
  102. var index = $(this).data('index');
  103. that.$fixedBody.find('tr[data-index="' + index + '"]').addClass('hover');
  104. }, function () {
  105. var index = $(this).data('index');
  106. that.$fixedBody.find('tr[data-index="' + index + '"]').removeClass('hover');
  107. });
  108. this.$fixedBody.find('tr[data-index]').off('hover').hover(function () {
  109. var index = $(this).data('index');
  110. that.$body.find('tr[data-index="' + index + '"]').addClass('hover');
  111. }, function () {
  112. var index = $(this).data('index');
  113. that.$body.find('> tr[data-index="' + index + '"]').removeClass('hover');
  114. });
  115. };
  116. })(jQuery);