bootstrap-table-reorder-columns.js 5.0 KB

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