UNPKG

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