仰望星辰工作室

better-staridc-MNBT

better-staridc-MNBT/ imsetes/js/bootstrap-table/extensions/fixed-columns/bootstrap-table-fixed-columns.js 10.9 KB · 395 行 原始文件
Z zfhsh first commit 2 个月前
1/**
2 * @author zhixin wen <wenzhixin2010@gmail.com>
3 */
4
5const Utils = $.fn.bootstrapTable.utils
6
7// Reasonable defaults
8const PIXEL_STEP = 10
9const LINE_HEIGHT = 40
10const PAGE_HEIGHT = 800
11
12function normalizeWheel (event) {
13 let sX = 0 // spinX
14 let sY = 0 // spinY
15 let pX = 0 // pixelX
16 let pY = 0 // pixelY
17
18 // Legacy
19 if ('detail' in event) { sY = event.detail }
20 if ('wheelDelta' in event) { sY = -event.wheelDelta / 120 }
21 if ('wheelDeltaY' in event) { sY = -event.wheelDeltaY / 120 }
22 if ('wheelDeltaX' in event) { sX = -event.wheelDeltaX / 120 }
23
24 // side scrolling on FF with DOMMouseScroll
25 if ( 'axis' in event && event.axis === event.HORIZONTAL_AXIS ) {
26 sX = sY
27 sY = 0
28 }
29
30 pX = sX * PIXEL_STEP
31 pY = sY * PIXEL_STEP
32
33 if ('deltaY' in event) { pY = event.deltaY }
34 if ('deltaX' in event) { pX = event.deltaX }
35
36 if ((pX || pY) && event.deltaMode) {
37 if (event.deltaMode === 1) { // delta in LINE units
38 pX *= LINE_HEIGHT
39 pY *= LINE_HEIGHT
40 } else { // delta in PAGE units
41 pX *= PAGE_HEIGHT
42 pY *= PAGE_HEIGHT
43 }
44 }
45
46 // Fall-back if spin cannot be determined
47 if (pX && !sX) { sX = (pX < 1) ? -1 : 1 }
48 if (pY && !sY) { sY = (pY < 1) ? -1 : 1 }
49
50 return {
51 spinX: sX,
52 spinY: sY,
53 pixelX: pX,
54 pixelY: pY
55 }
56}
57
58$.extend($.fn.bootstrapTable.defaults, {
59 fixedColumns: false,
60 fixedNumber: 0,
61 fixedRightNumber: 0
62})
63
64$.BootstrapTable = class extends $.BootstrapTable {
65
66 fixedColumnsSupported () {
67 return this.options.fixedColumns &&
68 !this.options.detailView &&
69 !this.options.cardView
70 }
71
72 initContainer () {
73 super.initContainer()
74
75 if (!this.fixedColumnsSupported()) {
76 return
77 }
78
79 if (this.options.fixedNumber) {
80 this.$tableContainer.append('<div class="fixed-columns"></div>')
81 this.$fixedColumns = this.$tableContainer.find('.fixed-columns')
82 }
83
84 if (this.options.fixedRightNumber) {
85 this.$tableContainer.append('<div class="fixed-columns-right"></div>')
86 this.$fixedColumnsRight = this.$tableContainer.find('.fixed-columns-right')
87 }
88 }
89
90 initBody (...args) {
91 super.initBody(...args)
92
93 if (!this.fixedColumnsSupported()) {
94 return
95 }
96
97 if (this.options.showHeader && this.options.height) {
98 return
99 }
100
101 this.initFixedColumnsBody()
102 this.initFixedColumnsEvents()
103 }
104
105 trigger (...args) {
106 super.trigger(...args)
107
108 if (!this.fixedColumnsSupported()) {
109 return
110 }
111
112 if (args[0] === 'post-header') {
113 this.initFixedColumnsHeader()
114 } else if (args[0] === 'scroll-body') {
115 if (this.needFixedColumns && this.options.fixedNumber) {
116 this.$fixedBody.scrollTop(this.$tableBody.scrollTop())
117 }
118
119 if (this.needFixedColumns && this.options.fixedRightNumber) {
120 this.$fixedBodyRight.scrollTop(this.$tableBody.scrollTop())
121 }
122 }
123 }
124
125 updateSelected () {
126 super.updateSelected()
127
128 if (!this.fixedColumnsSupported()) {
129 return
130 }
131
132 this.$tableBody.find('tr').each((i, el) => {
133 const $el = $(el)
134 const index = $el.data('index')
135 const classes = $el.attr('class')
136
137 const inputSelector = `[name="${this.options.selectItemName}"]`
138 const $input = $el.find(inputSelector)
139
140 if (typeof index === undefined) {
141 return
142 }
143
144 const updateFixedBody = ($fixedHeader, $fixedBody) => {
145 const $tr = $fixedBody.find(`tr[data-index="${index}"]`)
146 $tr.attr('class', classes)
147
148 if ($input.length) {
149 $tr.find(inputSelector).prop('checked', $input.prop('checked'))
150 }
151
152 if (this.$selectAll.length) {
153 $fixedHeader.add($fixedBody)
154 .find('[name="btSelectAll"]')
155 .prop('checked', this.$selectAll.prop('checked'))
156 }
157 }
158
159 if (this.$fixedBody && this.options.fixedNumber) {
160 updateFixedBody(this.$fixedHeader, this.$fixedBody)
161 }
162
163 if (this.$fixedBodyRight && this.options.fixedRightNumber) {
164 updateFixedBody(this.$fixedHeaderRight, this.$fixedBodyRight)
165 }
166 })
167 }
168
169 hideLoading () {
170 super.hideLoading()
171
172 if (this.needFixedColumns && this.options.fixedNumber) {
173 this.$fixedColumns.find('.fixed-table-loading').hide()
174 }
175
176 if (this.needFixedColumns && this.options.fixedRightNumber) {
177 this.$fixedColumnsRight.find('.fixed-table-loading').hide()
178 }
179 }
180
181 initFixedColumnsHeader () {
182 if (this.options.height) {
183 this.needFixedColumns = this.$tableHeader.outerWidth(true) < this.$tableHeader.find('table').outerWidth(true)
184 } else {
185 this.needFixedColumns = this.$tableBody.outerWidth(true) < this.$tableBody.find('table').outerWidth(true)
186 }
187
188 const initFixedHeader = ($fixedColumns, isRight) => {
189 $fixedColumns.find('.fixed-table-header').remove()
190 $fixedColumns.append(this.$tableHeader.clone(true))
191
192 $fixedColumns.css({
193 width: this.getFixedColumnsWidth(isRight)
194 })
195 return $fixedColumns.find('.fixed-table-header')
196 }
197
198 if (this.needFixedColumns && this.options.fixedNumber) {
199 this.$fixedHeader = initFixedHeader(this.$fixedColumns)
200 this.$fixedHeader.css('margin-right', '')
201 } else if (this.$fixedColumns) {
202 this.$fixedColumns.html('').css('width', '')
203 }
204
205 if (this.needFixedColumns && this.options.fixedRightNumber) {
206 this.$fixedHeaderRight = initFixedHeader(this.$fixedColumnsRight, true)
207 this.$fixedHeaderRight.scrollLeft(this.$fixedHeaderRight.find('table').width())
208 } else if (this.$fixedColumnsRight) {
209 this.$fixedColumnsRight.html('').css('width', '')
210 }
211
212 this.initFixedColumnsBody()
213 this.initFixedColumnsEvents()
214 }
215
216 initFixedColumnsBody () {
217 const initFixedBody = ($fixedColumns, $fixedHeader) => {
218 $fixedColumns.find('.fixed-table-body').remove()
219 $fixedColumns.append(this.$tableBody.clone(true))
220
221 const $fixedBody = $fixedColumns.find('.fixed-table-body')
222
223 const tableBody = this.$tableBody.get(0)
224 const scrollHeight = tableBody.scrollWidth > tableBody.clientWidth
225 ? Utils.getScrollBarWidth() : 0
226 const height = this.$tableContainer.outerHeight(true) - scrollHeight - 1
227
228 $fixedColumns.css({
229 height
230 })
231
232 $fixedBody.css({
233 height: height - $fixedHeader.height()
234 })
235
236 return $fixedBody
237 }
238
239 if (this.needFixedColumns && this.options.fixedNumber) {
240 this.$fixedBody = initFixedBody(this.$fixedColumns, this.$fixedHeader)
241 }
242
243 if (this.needFixedColumns && this.options.fixedRightNumber) {
244 this.$fixedBodyRight = initFixedBody(this.$fixedColumnsRight, this.$fixedHeaderRight)
245 this.$fixedBodyRight.scrollLeft(this.$fixedBodyRight.find('table').width())
246 this.$fixedBodyRight.css('overflow-y', this.options.height ? 'auto' : 'hidden')
247 }
248 }
249
250 getFixedColumnsWidth (isRight) {
251 let visibleFields = this.getVisibleFields()
252 let width = 0
253 let fixedNumber = this.options.fixedNumber
254 let marginRight = 0
255
256 if (isRight) {
257 visibleFields = visibleFields.reverse()
258 fixedNumber = this.options.fixedRightNumber
259 marginRight = parseInt(this.$tableHeader.css('margin-right'), 10)
260 }
261
262 for (let i = 0; i < fixedNumber; i++) {
263 width += this.$header.find(`th[data-field="${visibleFields[i]}"]`).outerWidth(true)
264 }
265
266 return width + marginRight + 1
267 }
268
269 initFixedColumnsEvents () {
270 const toggleHover = (e, toggle) => {
271 const tr = `tr[data-index="${$(e.currentTarget).data('index')}"]`
272 let $trs = this.$tableBody.find(tr)
273
274 if (this.$fixedBody) {
275 $trs = $trs.add(this.$fixedBody.find(tr))
276 }
277 if (this.$fixedBodyRight) {
278 $trs = $trs.add(this.$fixedBodyRight.find(tr))
279 }
280
281 $trs.css('background-color', toggle ? $(e.currentTarget).css('background-color') : '')
282 }
283
284 this.$tableBody.find('tr').hover(e => {
285 toggleHover(e, true)
286 }, e => {
287 toggleHover(e, false)
288 })
289
290 const isFirefox = typeof navigator !== 'undefined' &&
291 navigator.userAgent.toLowerCase().indexOf('firefox') > -1
292 const mousewheel = isFirefox ? 'DOMMouseScroll' : 'mousewheel'
293 const updateScroll = (e, fixedBody) => {
294 const normalized = normalizeWheel(e)
295 const deltaY = Math.ceil(normalized.pixelY)
296 const top = this.$tableBody.scrollTop() + deltaY
297
298 if (
299 deltaY < 0 && top > 0 ||
300 deltaY > 0 && top < fixedBody.scrollHeight - fixedBody.clientHeight
301 ) {
302 e.preventDefault()
303 }
304
305 this.$tableBody.scrollTop(top)
306 if (this.$fixedBody) {
307 this.$fixedBody.scrollTop(top)
308 }
309 if (this.$fixedBodyRight) {
310 this.$fixedBodyRight.scrollTop(top)
311 }
312 }
313
314 if (this.needFixedColumns && this.options.fixedNumber) {
315 this.$fixedBody.find('tr').hover(e => {
316 toggleHover(e, true)
317 }, e => {
318 toggleHover(e, false)
319 })
320
321 this.$fixedBody[0].addEventListener(mousewheel, e => {
322 updateScroll(e, this.$fixedBody[0])
323 })
324 }
325
326 if (this.needFixedColumns && this.options.fixedRightNumber) {
327 this.$fixedBodyRight.find('tr').hover(e => {
328 toggleHover(e, true)
329 }, e => {
330 toggleHover(e, false)
331 })
332
333 this.$fixedBodyRight.off('scroll').on('scroll', () => {
334 const top = this.$fixedBodyRight.scrollTop()
335
336 this.$tableBody.scrollTop(top)
337 if (this.$fixedBody) {
338 this.$fixedBody.scrollTop(top)
339 }
340 })
341 }
342
343 if (this.options.filterControl) {
344 $(this.$fixedColumns).off('keyup change').on('keyup change', e => {
345 const $target = $(e.target)
346 const value = $target.val()
347 const field = $target.parents('th').data('field')
348 const $coreTh = this.$header.find(`th[data-field="${field}"]`)
349
350 if ($target.is('input')) {
351 $coreTh.find('input').val(value)
352 } else if ($target.is('select')) {
353 const $select = $coreTh.find('select')
354 $select.find('option[selected]').removeAttr('selected')
355 $select.find(`option[value="${value}"]`).attr('selected', true)
356 }
357
358 this.triggerSearch()
359 })
360 }
361 }
362
363 renderStickyHeader () {
364 if (!this.options.stickyHeader) {
365 return
366 }
367
368 this.$stickyContainer = this.$container.find('.sticky-header-container')
369 super.renderStickyHeader()
370
371 if (this.needFixedColumns && this.options.fixedNumber) {
372 this.$fixedColumns.css('z-index', 101)
373 .find('.sticky-header-container')
374 .css('right', '')
375 .width(this.$fixedColumns.outerWidth())
376 }
377
378 if (this.needFixedColumns && this.options.fixedRightNumber) {
379 const $stickyHeaderContainerRight = this.$fixedColumnsRight.find('.sticky-header-container')
380
381 this.$fixedColumnsRight.css('z-index', 101)
382 $stickyHeaderContainerRight.css('left', '')
383 .scrollLeft($stickyHeaderContainerRight.find('.table').outerWidth())
384 .width(this.$fixedColumnsRight.outerWidth())
385 }
386 }
387
388 matchPositionX () {
389 if (!this.options.stickyHeader) {
390 return
391 }
392
393 this.$stickyContainer.eq(0).scrollLeft(this.$tableBody.scrollLeft())
394 }
395}