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