bootstrap-table-reorder-columns.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /**
  2. * @author: Dennis Hernández
  3. * @webSite: http://djhvscf.github.io/Blog
  4. * @version: v1.1.0
  5. */
  6. !function ($) {
  7. 'use strict';
  8. var getFieldIndex = function (columns, field) {
  9. var index = -1;
  10. $.each(columns, function (i, column) {
  11. if (column.field === field) {
  12. index = i;
  13. return false;
  14. }
  15. return true;
  16. });
  17. return index;
  18. };
  19. $.extend($.fn.bootstrapTable.defaults, {
  20. reorderableColumns: false,
  21. maxMovingRows: 10,
  22. onReorderColumn: function (headerFields) {
  23. return false;
  24. },
  25. dragaccept: null
  26. });
  27. $.extend($.fn.bootstrapTable.Constructor.EVENTS, {
  28. 'reorder-column.bs.table': 'onReorderColumn'
  29. });
  30. var BootstrapTable = $.fn.bootstrapTable.Constructor,
  31. _initHeader = BootstrapTable.prototype.initHeader,
  32. _toggleColumn = BootstrapTable.prototype.toggleColumn,
  33. _toggleView = BootstrapTable.prototype.toggleView,
  34. _resetView = BootstrapTable.prototype.resetView;
  35. BootstrapTable.prototype.initHeader = function () {
  36. _initHeader.apply(this, Array.prototype.slice.apply(arguments));
  37. if (!this.options.reorderableColumns) {
  38. return;
  39. }
  40. this.makeRowsReorderable();
  41. };
  42. BootstrapTable.prototype.toggleColumn = function () {
  43. _toggleColumn.apply(this, Array.prototype.slice.apply(arguments));
  44. if (!this.options.reorderableColumns) {
  45. return;
  46. }
  47. this.makeRowsReorderable();
  48. };
  49. BootstrapTable.prototype.toggleView = function () {
  50. _toggleView.apply(this, Array.prototype.slice.apply(arguments));
  51. if (!this.options.reorderableColumns) {
  52. return;
  53. }
  54. if (this.options.cardView) {
  55. return;
  56. }
  57. this.makeRowsReorderable();
  58. };
  59. BootstrapTable.prototype.resetView = function () {
  60. _resetView.apply(this, Array.prototype.slice.apply(arguments));
  61. if (!this.options.reorderableColumns) {
  62. return;
  63. }
  64. this.makeRowsReorderable();
  65. };
  66. BootstrapTable.prototype.makeRowsReorderable = function () {
  67. var that = this;
  68. try {
  69. $(this.$el).dragtable('destroy');
  70. } catch (e) {}
  71. $(this.$el).dragtable({
  72. maxMovingRows: that.options.maxMovingRows,
  73. dragaccept: that.options.dragaccept,
  74. clickDelay:200,
  75. beforeStop: function() {
  76. var ths = [],
  77. formatters = [],
  78. columns = [],
  79. columnsHidden = [],
  80. columnIndex = -1;
  81. that.$header.find('th').each(function (i) {
  82. ths.push($(this).data('field'));
  83. formatters.push($(this).data('formatter'));
  84. });
  85. //Exist columns not shown
  86. if (ths.length < that.columns.length) {
  87. columnsHidden = $.grep(that.columns, function (column) {
  88. return !column.visible;
  89. });
  90. for (var i = 0; i < columnsHidden.length; i++) {
  91. ths.push(columnsHidden[i].field);
  92. formatters.push(columnsHidden[i].formatter);
  93. }
  94. }
  95. for (var i = 0; i < ths.length; i++ ) {
  96. columnIndex = getFieldIndex(that.columns, ths[i]);
  97. if (columnIndex !== -1) {
  98. columns.push(that.columns[columnIndex]);
  99. that.columns.splice(columnIndex, 1);
  100. }
  101. }
  102. that.columns = that.columns.concat(columns);
  103. that.header.fields = ths;
  104. that.header.formatters = formatters;
  105. that.resetView();
  106. that.trigger('reorder-column', ths);
  107. }
  108. });
  109. };
  110. }(jQuery);