-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
/
bootstrap-table-sticky-header.js
153 lines (127 loc) · 5.45 KB
/
bootstrap-table-sticky-header.js
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
/**
* @author vincent loh <vincent.ml@gmail.com>
* @update J Manuel Corona <jmcg92@gmail.com>
* @update zhixin wen <wenzhixin2010@gmail.com>
*/
const Utils = $.fn.bootstrapTable.utils
Object.assign($.fn.bootstrapTable.defaults, {
stickyHeader: false,
stickyHeaderOffsetY: 0,
stickyHeaderOffsetLeft: 0,
stickyHeaderOffsetRight: 0
})
$.BootstrapTable = class extends $.BootstrapTable {
initHeader (...args) {
super.initHeader(...args)
if (!this.options.stickyHeader) {
return
}
this.$tableBody.find('.sticky-header-container,.sticky_anchor_begin,.sticky_anchor_end').remove()
this.$el.before('<div class="sticky-header-container"></div>')
this.$el.before('<div class="sticky_anchor_begin"></div>')
this.$el.after('<div class="sticky_anchor_end"></div>')
this.$header.addClass('sticky-header')
// clone header just once, to be used as sticky header
// deep clone header, using source header affects tbody>td width
this.$stickyContainer = this.$tableBody.find('.sticky-header-container')
this.$stickyBegin = this.$tableBody.find('.sticky_anchor_begin')
this.$stickyEnd = this.$tableBody.find('.sticky_anchor_end')
this.$stickyHeader = this.$header.clone(true, true)
// render sticky on window scroll or resize
const resizeEvent = Utils.getEventName('resize.sticky-header-table', this.$el.attr('id'))
const scrollEvent = Utils.getEventName('scroll.sticky-header-table', this.$el.attr('id'))
$(window).off(resizeEvent).on(resizeEvent, () => this.renderStickyHeader())
$(window).off(scrollEvent).on(scrollEvent, () => this.renderStickyHeader())
this.$tableBody.off('scroll').on('scroll', () => this.matchPositionX())
}
onColumnSearch ({ currentTarget, keyCode }) {
super.onColumnSearch({ currentTarget, keyCode })
this.renderStickyHeader()
}
resetView (...args) {
super.resetView(...args)
$('.bootstrap-table.fullscreen').off('scroll')
.on('scroll', () => this.renderStickyHeader())
}
getCaret (...args) {
super.getCaret(...args)
if (this.$stickyHeader) {
const $ths = this.$stickyHeader.find('th')
this.$header.find('th').each((i, th) => {
$ths.eq(i).find('.sortable').attr('class', $(th).find('.sortable').attr('class'))
})
}
}
horizontalScroll () {
super.horizontalScroll()
this.$tableBody.on('scroll', () => this.matchPositionX())
}
renderStickyHeader () {
const that = this
this.$stickyHeader = this.$header.clone(true, true)
if (this.options.filterControl) {
$(this.$stickyHeader).off('keyup change mouseup').on('keyup change mouse', function (e) {
const $target = $(e.target)
const value = $target.val()
const field = $target.parents('th').data('field')
const $coreTh = that.$header.find(`th[data-field="${field}"]`)
if ($target.is('input')) {
$coreTh.find('input').val(value)
} else if ($target.is('select')) {
const $select = $coreTh.find('select')
$select.find('option[selected]').removeAttr('selected')
$select.find(`option[value="${value}"]`).attr('selected', true)
}
that.triggerSearch()
})
}
const top = $(window).scrollTop()
// top anchor scroll position, minus header height
const start = this.$stickyBegin.offset().top - this.options.stickyHeaderOffsetY
// bottom anchor scroll position, minus header height, minus sticky height
const end = this.$stickyEnd.offset().top - this.options.stickyHeaderOffsetY - this.$header.height()
// show sticky when top anchor touches header, and when bottom anchor not exceeded
if (top > start && top <= end) {
// ensure clone and source column widths are the same
this.$stickyHeader.find('tr').each((indexRows, rows) => {
$(rows).find('th').each((index, el) => {
$(el).css('min-width', this.$header.find(`tr:eq(${indexRows})`).find(`th:eq(${index})`).css('width'))
})
})
// match bootstrap table style
this.$stickyContainer.show().addClass('fix-sticky fixed-table-container')
// stick it in position
const coords = this.$tableBody[0].getBoundingClientRect()
let width = '100%'
let stickyHeaderOffsetLeft = this.options.stickyHeaderOffsetLeft
let stickyHeaderOffsetRight = this.options.stickyHeaderOffsetRight
if (!stickyHeaderOffsetLeft) {
stickyHeaderOffsetLeft = coords.left
}
if (!stickyHeaderOffsetRight) {
width = `${coords.width}px`
}
if (this.$el.closest('.bootstrap-table').hasClass('fullscreen')) {
stickyHeaderOffsetLeft = 0
stickyHeaderOffsetRight = 0
width = '100%'
}
this.$stickyContainer.css('top', `${this.options.stickyHeaderOffsetY}px`)
this.$stickyContainer.css('left', `${stickyHeaderOffsetLeft}px`)
this.$stickyContainer.css('right', `${stickyHeaderOffsetRight}px`)
this.$stickyContainer.css('width', `${width}`)
// create scrollable container for header
this.$stickyTable = $('<table/>')
this.$stickyTable.addClass(this.options.classes)
// append cloned header to dom
this.$stickyContainer.html(this.$stickyTable.append(this.$stickyHeader))
// match clone and source header positions when left-right scroll
this.matchPositionX()
} else {
this.$stickyContainer.removeClass('fix-sticky').hide()
}
}
matchPositionX () {
this.$stickyContainer.scrollLeft(this.$tableBody.scrollLeft())
}
}