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