bootstrap-table-fixed-columns.js 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /**
  2. * @author zhixin wen <wenzhixin2010@gmail.com>
  3. */
  4. const Utils = $.fn.bootstrapTable.utils
  5. // Reasonable defaults
  6. const PIXEL_STEP = 10
  7. const LINE_HEIGHT = 40
  8. const PAGE_HEIGHT = 800
  9. function normalizeWheel (event) {
  10. let sX = 0 // spinX
  11. let sY = 0 // spinY
  12. let pX = 0 // pixelX
  13. let pY = 0 // pixelY
  14. // Legacy
  15. if ('detail' in event) { sY = event.detail }
  16. if ('wheelDelta' in event) { sY = -event.wheelDelta / 120 }
  17. if ('wheelDeltaY' in event) { sY = -event.wheelDeltaY / 120 }
  18. if ('wheelDeltaX' in event) { sX = -event.wheelDeltaX / 120 }
  19. // side scrolling on FF with DOMMouseScroll
  20. if ( 'axis' in event && event.axis === event.HORIZONTAL_AXIS ) {
  21. sX = sY
  22. sY = 0
  23. }
  24. pX = sX * PIXEL_STEP
  25. pY = sY * PIXEL_STEP
  26. if ('deltaY' in event) { pY = event.deltaY }
  27. if ('deltaX' in event) { pX = event.deltaX }
  28. if ((pX || pY) && event.deltaMode) {
  29. if (event.deltaMode === 1) { // delta in LINE units
  30. pX *= LINE_HEIGHT
  31. pY *= LINE_HEIGHT
  32. } else { // delta in PAGE units
  33. pX *= PAGE_HEIGHT
  34. pY *= PAGE_HEIGHT
  35. }
  36. }
  37. // Fall-back if spin cannot be determined
  38. if (pX && !sX) { sX = (pX < 1) ? -1 : 1 }
  39. if (pY && !sY) { sY = (pY < 1) ? -1 : 1 }
  40. return {
  41. spinX: sX,
  42. spinY: sY,
  43. pixelX: pX,
  44. pixelY: pY
  45. }
  46. }
  47. $.extend($.fn.bootstrapTable.defaults, {
  48. fixedColumns: false,
  49. fixedNumber: 0,
  50. fixedRightNumber: 0
  51. })
  52. $.BootstrapTable = class extends $.BootstrapTable {
  53. fixedColumnsSupported () {
  54. return this.options.fixedColumns &&
  55. !this.options.detailView &&
  56. !this.options.cardView
  57. }
  58. initContainer () {
  59. super.initContainer()
  60. if (!this.fixedColumnsSupported()) {
  61. return
  62. }
  63. if (this.options.fixedNumber) {
  64. this.$tableContainer.append('<div class="fixed-columns"></div>')
  65. this.$fixedColumns = this.$tableContainer.find('.fixed-columns')
  66. }
  67. if (this.options.fixedRightNumber) {
  68. this.$tableContainer.append('<div class="fixed-columns-right"></div>')
  69. this.$fixedColumnsRight = this.$tableContainer.find('.fixed-columns-right')
  70. }
  71. }
  72. initBody (...args) {
  73. super.initBody(...args)
  74. if (!this.fixedColumnsSupported()) {
  75. return
  76. }
  77. if (this.options.showHeader && this.options.height) {
  78. return
  79. }
  80. this.initFixedColumnsBody()
  81. this.initFixedColumnsEvents()
  82. }
  83. trigger (...args) {
  84. super.trigger(...args)
  85. if (!this.fixedColumnsSupported()) {
  86. return
  87. }
  88. if (args[0] === 'post-header') {
  89. this.initFixedColumnsHeader()
  90. } else if (args[0] === 'scroll-body') {
  91. if (this.needFixedColumns && this.options.fixedNumber) {
  92. this.$fixedBody.scrollTop(this.$tableBody.scrollTop())
  93. }
  94. if (this.needFixedColumns && this.options.fixedRightNumber) {
  95. this.$fixedBodyRight.scrollTop(this.$tableBody.scrollTop())
  96. }
  97. }
  98. }
  99. updateSelected () {
  100. super.updateSelected()
  101. if (!this.fixedColumnsSupported()) {
  102. return
  103. }
  104. this.$tableBody.find('tr').each((i, el) => {
  105. const $el = $(el)
  106. const index = $el.data('index')
  107. const classes = $el.attr('class')
  108. const inputSelector = `[name="${this.options.selectItemName}"]`
  109. const $input = $el.find(inputSelector)
  110. if (typeof index === undefined) {
  111. return
  112. }
  113. const updateFixedBody = ($fixedHeader, $fixedBody) => {
  114. const $tr = $fixedBody.find(`tr[data-index="${index}"]`)
  115. $tr.attr('class', classes)
  116. if ($input.length) {
  117. $tr.find(inputSelector).prop('checked', $input.prop('checked'))
  118. }
  119. if (this.$selectAll.length) {
  120. $fixedHeader.add($fixedBody)
  121. .find('[name="btSelectAll"]')
  122. .prop('checked', this.$selectAll.prop('checked'))
  123. }
  124. }
  125. if (this.$fixedBody && this.options.fixedNumber) {
  126. updateFixedBody(this.$fixedHeader, this.$fixedBody)
  127. }
  128. if (this.$fixedBodyRight && this.options.fixedRightNumber) {
  129. updateFixedBody(this.$fixedHeaderRight, this.$fixedBodyRight)
  130. }
  131. })
  132. }
  133. hideLoading () {
  134. super.hideLoading()
  135. if (this.needFixedColumns && this.options.fixedNumber) {
  136. this.$fixedColumns.find('.fixed-table-loading').hide()
  137. }
  138. if (this.needFixedColumns && this.options.fixedRightNumber) {
  139. this.$fixedColumnsRight.find('.fixed-table-loading').hide()
  140. }
  141. }
  142. initFixedColumnsHeader () {
  143. if (this.options.height) {
  144. this.needFixedColumns = this.$tableHeader.outerWidth(true) < this.$tableHeader.find('table').outerWidth(true)
  145. } else {
  146. this.needFixedColumns = this.$tableBody.outerWidth(true) < this.$tableBody.find('table').outerWidth(true)
  147. }
  148. const initFixedHeader = ($fixedColumns, isRight) => {
  149. $fixedColumns.find('.fixed-table-header').remove()
  150. $fixedColumns.append(this.$tableHeader.clone(true))
  151. $fixedColumns.css({
  152. width: this.getFixedColumnsWidth(isRight)
  153. })
  154. return $fixedColumns.find('.fixed-table-header')
  155. }
  156. if (this.needFixedColumns && this.options.fixedNumber) {
  157. this.$fixedHeader = initFixedHeader(this.$fixedColumns)
  158. this.$fixedHeader.css('margin-right', '')
  159. } else if (this.$fixedColumns) {
  160. this.$fixedColumns.html('').css('width', '')
  161. }
  162. if (this.needFixedColumns && this.options.fixedRightNumber) {
  163. this.$fixedHeaderRight = initFixedHeader(this.$fixedColumnsRight, true)
  164. this.$fixedHeaderRight.scrollLeft(this.$fixedHeaderRight.find('table').width())
  165. } else if (this.$fixedColumnsRight) {
  166. this.$fixedColumnsRight.html('').css('width', '')
  167. }
  168. this.initFixedColumnsBody()
  169. this.initFixedColumnsEvents()
  170. }
  171. initFixedColumnsBody () {
  172. const initFixedBody = ($fixedColumns, $fixedHeader) => {
  173. $fixedColumns.find('.fixed-table-body').remove()
  174. $fixedColumns.append(this.$tableBody.clone(true))
  175. const $fixedBody = $fixedColumns.find('.fixed-table-body')
  176. const tableBody = this.$tableBody.get(0)
  177. const scrollHeight = tableBody.scrollWidth > tableBody.clientWidth
  178. ? Utils.getScrollBarWidth() : 0
  179. const height = this.$tableContainer.outerHeight(true) - scrollHeight - 1
  180. $fixedColumns.css({
  181. height
  182. })
  183. $fixedBody.css({
  184. height: height - $fixedHeader.height()
  185. })
  186. return $fixedBody
  187. }
  188. if (this.needFixedColumns && this.options.fixedNumber) {
  189. this.$fixedBody = initFixedBody(this.$fixedColumns, this.$fixedHeader)
  190. }
  191. if (this.needFixedColumns && this.options.fixedRightNumber) {
  192. this.$fixedBodyRight = initFixedBody(this.$fixedColumnsRight, this.$fixedHeaderRight)
  193. this.$fixedBodyRight.scrollLeft(this.$fixedBodyRight.find('table').width())
  194. this.$fixedBodyRight.css('overflow-y', this.options.height ? 'auto' : 'hidden')
  195. }
  196. }
  197. getFixedColumnsWidth (isRight) {
  198. let visibleFields = this.getVisibleFields()
  199. let width = 0
  200. let fixedNumber = this.options.fixedNumber
  201. let marginRight = 0
  202. if (isRight) {
  203. visibleFields = visibleFields.reverse()
  204. fixedNumber = this.options.fixedRightNumber
  205. marginRight = parseInt(this.$tableHeader.css('margin-right'), 10)
  206. }
  207. for (let i = 0; i < fixedNumber; i++) {
  208. width += this.$header.find(`th[data-field="${visibleFields[i]}"]`).outerWidth(true)
  209. }
  210. return width + marginRight + 1
  211. }
  212. initFixedColumnsEvents () {
  213. const toggleHover = (e, toggle) => {
  214. const tr = `tr[data-index="${$(e.currentTarget).data('index')}"]`
  215. let $trs = this.$tableBody.find(tr)
  216. if (this.$fixedBody) {
  217. $trs = $trs.add(this.$fixedBody.find(tr))
  218. }
  219. if (this.$fixedBodyRight) {
  220. $trs = $trs.add(this.$fixedBodyRight.find(tr))
  221. }
  222. $trs.css('background-color', toggle ? $(e.currentTarget).css('background-color') : '')
  223. }
  224. this.$tableBody.find('tr').hover(e => {
  225. toggleHover(e, true)
  226. }, e => {
  227. toggleHover(e, false)
  228. })
  229. const isFirefox = typeof navigator !== 'undefined' &&
  230. navigator.userAgent.toLowerCase().indexOf('firefox') > -1
  231. const mousewheel = isFirefox ? 'DOMMouseScroll' : 'mousewheel'
  232. const updateScroll = (e, fixedBody) => {
  233. const normalized = normalizeWheel(e)
  234. const deltaY = Math.ceil(normalized.pixelY)
  235. const top = this.$tableBody.scrollTop() + deltaY
  236. if (
  237. deltaY < 0 && top > 0 ||
  238. deltaY > 0 && top < fixedBody.scrollHeight - fixedBody.clientHeight
  239. ) {
  240. e.preventDefault()
  241. }
  242. this.$tableBody.scrollTop(top)
  243. if (this.$fixedBody) {
  244. this.$fixedBody.scrollTop(top)
  245. }
  246. if (this.$fixedBodyRight) {
  247. this.$fixedBodyRight.scrollTop(top)
  248. }
  249. }
  250. if (this.needFixedColumns && this.options.fixedNumber) {
  251. this.$fixedBody.find('tr').hover(e => {
  252. toggleHover(e, true)
  253. }, e => {
  254. toggleHover(e, false)
  255. })
  256. this.$fixedBody[0].addEventListener(mousewheel, e => {
  257. updateScroll(e, this.$fixedBody[0])
  258. })
  259. }
  260. if (this.needFixedColumns && this.options.fixedRightNumber) {
  261. this.$fixedBodyRight.find('tr').hover(e => {
  262. toggleHover(e, true)
  263. }, e => {
  264. toggleHover(e, false)
  265. })
  266. this.$fixedBodyRight.off('scroll').on('scroll', () => {
  267. const top = this.$fixedBodyRight.scrollTop()
  268. this.$tableBody.scrollTop(top)
  269. if (this.$fixedBody) {
  270. this.$fixedBody.scrollTop(top)
  271. }
  272. })
  273. }
  274. if (this.options.filterControl) {
  275. $(this.$fixedColumns).off('keyup change').on('keyup change', e => {
  276. const $target = $(e.target)
  277. const value = $target.val()
  278. const field = $target.parents('th').data('field')
  279. const $coreTh = this.$header.find(`th[data-field="${field}"]`)
  280. if ($target.is('input')) {
  281. $coreTh.find('input').val(value)
  282. } else if ($target.is('select')) {
  283. const $select = $coreTh.find('select')
  284. $select.find('option[selected]').removeAttr('selected')
  285. $select.find(`option[value="${value}"]`).attr('selected', true)
  286. }
  287. this.triggerSearch()
  288. })
  289. }
  290. }
  291. }