bootstrap-table-reorder-columns.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. const BootstrapTable = $.fn.bootstrapTable.Constructor
  49. const _initHeader = BootstrapTable.prototype.initHeader
  50. const _toggleColumn = BootstrapTable.prototype.toggleColumn
  51. const _toggleView = BootstrapTable.prototype.toggleView
  52. const _resetView = BootstrapTable.prototype.resetView
  53. BootstrapTable.prototype.initHeader = function (...args) {
  54. _initHeader.apply(this, Array.prototype.slice.apply(args))
  55. if (!this.options.reorderableColumns) {
  56. return
  57. }
  58. this.makeRowsReorderable()
  59. }
  60. BootstrapTable.prototype.toggleColumn = function (...args) {
  61. _toggleColumn.apply(this, Array.prototype.slice.apply(args))
  62. if (!this.options.reorderableColumns) {
  63. return
  64. }
  65. this.makeRowsReorderable()
  66. }
  67. BootstrapTable.prototype.toggleView = function (...args) {
  68. _toggleView.apply(this, Array.prototype.slice.apply(args))
  69. if (!this.options.reorderableColumns) {
  70. return
  71. }
  72. if (this.options.cardView) {
  73. return
  74. }
  75. this.makeRowsReorderable()
  76. }
  77. BootstrapTable.prototype.resetView = function (...args) {
  78. _resetView.apply(this, Array.prototype.slice.apply(args))
  79. if (!this.options.reorderableColumns) {
  80. return
  81. }
  82. this.makeRowsReorderable()
  83. }
  84. BootstrapTable.prototype.makeRowsReorderable = function () {
  85. const that = this
  86. try {
  87. $(this.$el).dragtable('destroy')
  88. } catch (e) {
  89. //
  90. }
  91. $(this.$el).dragtable({
  92. maxMovingRows: that.options.maxMovingRows,
  93. dragaccept: that.options.dragaccept,
  94. clickDelay: 200,
  95. beforeStop () {
  96. const ths = []
  97. const formatters = []
  98. const columns = []
  99. let columnsHidden = []
  100. let columnIndex = -1
  101. const optionsColumns = []
  102. that.$header.find('th').each(function (i) {
  103. ths.push($(this).data('field'))
  104. formatters.push($(this).data('formatter'))
  105. })
  106. // Exist columns not shown
  107. if (ths.length < that.columns.length) {
  108. columnsHidden = that.columns.filter(column => !column.visible)
  109. for (var i = 0; i < columnsHidden.length; i++) {
  110. ths.push(columnsHidden[i].field)
  111. formatters.push(columnsHidden[i].formatter)
  112. }
  113. }
  114. for (let i = 0; i < ths.length; i++ ) {
  115. columnIndex = that.fieldsColumnsIndex[ths[i]]
  116. if (columnIndex !== -1) {
  117. that.fieldsColumnsIndex[ths[i]] = i
  118. that.columns[columnIndex].fieldIndex = i
  119. columns.push(that.columns[columnIndex])
  120. }
  121. }
  122. that.columns = columns
  123. filterFn() // Support <IE9
  124. $.each(that.columns, (i, column) => {
  125. let found = false
  126. const field = column.field
  127. that.options.columns[0].filter(item => {
  128. if (!found && item['field'] === field) {
  129. optionsColumns.push(item)
  130. found = true
  131. return false
  132. }
  133. return true
  134. })
  135. })
  136. that.options.columns[0] = optionsColumns
  137. that.header.fields = ths
  138. that.header.formatters = formatters
  139. that.initHeader()
  140. that.initBody()
  141. that.resetView()
  142. that.trigger('reorder-column', ths)
  143. }
  144. })
  145. }