bootstrap-table-reorder-columns.js 5.0 KB

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