bootstrap-table-reorder-columns.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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.bootstrapTableReorderColumns = mod.exports;
  12. }
  13. })(this, function () {
  14. 'use strict';
  15. /**
  16. * @author: Dennis Hernández
  17. * @webSite: http://djhvscf.github.io/Blog
  18. * @version: v1.1.0
  19. */
  20. !function ($) {
  21. 'use strict';
  22. //From MDN site, https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter
  23. var filterFn = function filterFn() {
  24. if (!Array.prototype.filter) {
  25. Array.prototype.filter = function (fun /*, thisArg*/) {
  26. 'use strict';
  27. if (this === void 0 || this === null) {
  28. throw new TypeError();
  29. }
  30. var t = Object(this);
  31. var len = t.length >>> 0;
  32. if (typeof fun !== 'function') {
  33. throw new TypeError();
  34. }
  35. var res = [];
  36. var thisArg = arguments.length >= 2 ? arguments[1] : void 0;
  37. for (var i = 0; i < len; i++) {
  38. if (i in t) {
  39. var val = t[i];
  40. // NOTE: Technically this should Object.defineProperty at
  41. // the next index, as push can be affected by
  42. // properties on Object.prototype and Array.prototype.
  43. // But that method's new, and collisions should be
  44. // rare, so use the more-compatible alternative.
  45. if (fun.call(thisArg, val, i, t)) {
  46. res.push(val);
  47. }
  48. }
  49. }
  50. return res;
  51. };
  52. }
  53. };
  54. $.extend($.fn.bootstrapTable.defaults, {
  55. reorderableColumns: false,
  56. maxMovingRows: 10,
  57. onReorderColumn: function onReorderColumn(headerFields) {
  58. return false;
  59. },
  60. dragaccept: null
  61. });
  62. $.extend($.fn.bootstrapTable.Constructor.EVENTS, {
  63. 'reorder-column.bs.table': 'onReorderColumn'
  64. });
  65. var BootstrapTable = $.fn.bootstrapTable.Constructor,
  66. _initHeader = BootstrapTable.prototype.initHeader,
  67. _toggleColumn = BootstrapTable.prototype.toggleColumn,
  68. _toggleView = BootstrapTable.prototype.toggleView,
  69. _resetView = BootstrapTable.prototype.resetView;
  70. BootstrapTable.prototype.initHeader = function () {
  71. _initHeader.apply(this, Array.prototype.slice.apply(arguments));
  72. if (!this.options.reorderableColumns) {
  73. return;
  74. }
  75. this.makeRowsReorderable();
  76. };
  77. BootstrapTable.prototype.toggleColumn = function () {
  78. _toggleColumn.apply(this, Array.prototype.slice.apply(arguments));
  79. if (!this.options.reorderableColumns) {
  80. return;
  81. }
  82. this.makeRowsReorderable();
  83. };
  84. BootstrapTable.prototype.toggleView = function () {
  85. _toggleView.apply(this, Array.prototype.slice.apply(arguments));
  86. if (!this.options.reorderableColumns) {
  87. return;
  88. }
  89. if (this.options.cardView) {
  90. return;
  91. }
  92. this.makeRowsReorderable();
  93. };
  94. BootstrapTable.prototype.resetView = function () {
  95. _resetView.apply(this, Array.prototype.slice.apply(arguments));
  96. if (!this.options.reorderableColumns) {
  97. return;
  98. }
  99. this.makeRowsReorderable();
  100. };
  101. BootstrapTable.prototype.makeRowsReorderable = function () {
  102. var that = this;
  103. try {
  104. $(this.$el).dragtable('destroy');
  105. } catch (e) {}
  106. $(this.$el).dragtable({
  107. maxMovingRows: that.options.maxMovingRows,
  108. dragaccept: that.options.dragaccept,
  109. clickDelay: 200,
  110. beforeStop: function beforeStop() {
  111. var ths = [],
  112. formatters = [],
  113. columns = [],
  114. columnsHidden = [],
  115. columnIndex = -1,
  116. optionsColumns = [];
  117. that.$header.find('th').each(function (i) {
  118. ths.push($(this).data('field'));
  119. formatters.push($(this).data('formatter'));
  120. });
  121. //Exist columns not shown
  122. if (ths.length < that.columns.length) {
  123. columnsHidden = $.grep(that.columns, function (column) {
  124. return !column.visible;
  125. });
  126. for (var i = 0; i < columnsHidden.length; i++) {
  127. ths.push(columnsHidden[i].field);
  128. formatters.push(columnsHidden[i].formatter);
  129. }
  130. }
  131. for (var i = 0; i < this.length; i++) {
  132. columnIndex = that.fieldsColumnsIndex[ths[i]];
  133. if (columnIndex !== -1) {
  134. that.columns[columnIndex].fieldIndex = i;
  135. columns.push(that.columns[columnIndex]);
  136. that.columns.splice(columnIndex, 1);
  137. }
  138. }
  139. that.columns = that.columns.concat(columns);
  140. filterFn(); //Support <IE9
  141. $.each(that.columns, function (i, column) {
  142. var found = false,
  143. field = column.field;
  144. that.options.columns[0].filter(function (item) {
  145. if (!found && item["field"] == field) {
  146. optionsColumns.push(item);
  147. found = true;
  148. return false;
  149. } else return true;
  150. });
  151. });
  152. that.options.columns[0] = optionsColumns;
  153. that.header.fields = ths;
  154. that.header.formatters = formatters;
  155. that.initHeader();
  156. that.initToolbar();
  157. that.initBody();
  158. that.resetView();
  159. that.trigger('reorder-column', ths);
  160. }
  161. });
  162. };
  163. }(jQuery);
  164. });