bootstrap-table-addrbar.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /**
  2. * @author: general
  3. * @website: note.generals.space
  4. * @email: generals.space@gmail.com
  5. * @github: https://github.com/generals-space/bootstrap-table-addrbar
  6. * @update: zhixin wen <wenzhixin2010@gmail.com>
  7. */
  8. /*
  9. * function: 获取浏览器地址栏中的指定参数.
  10. * key: 参数名
  11. * url: 默认为当前地址栏
  12. */
  13. function _GET (key, url = window.location.search) {
  14. /*
  15. * 注意这里正则表达式的书写方法
  16. * (^|&)key匹配: 直接以key开始或以&key开始的字符串
  17. * 同理(&|$)表示以&结束或是直接结束的字符串
  18. * ...当然, 我并不知道这种用法.
  19. */
  20. const reg = new RegExp(`(^|&)${key}=([^&]*)(&|$)`)
  21. const result = url.substr(1).match(reg)
  22. if (result) {
  23. return decodeURIComponent(result[2])
  24. }
  25. return null
  26. }
  27. /*
  28. * function: 根据给定参数生成url地址
  29. * var dic = {name: 'genreal', age: 24}
  30. * var url = 'https://www.baidu.com?age=22';
  31. * _buildUrl(dic, url);
  32. * 将得到"https://www.baidu.com?age=24&name=genreal"
  33. * 哦, 忽略先后顺序吧...
  34. *
  35. * 补充: 可以参考浏览器URLSearchParams对象, 更加方便和强大.
  36. * 考虑到兼容性, 暂时不使用这个工具.
  37. */
  38. function _buildUrl (dict, url = window.location.search) {
  39. for (const [key, val] of Object.entries(dict)) {
  40. // 搜索name=general这种形式的字符串(&是分隔符)
  41. const pattern = `${key}=([^&]*)`
  42. const targetStr = `${key}=${val}`
  43. if (val === undefined)
  44. continue
  45. /*
  46. * 如果目标url中包含了key键, 我们需要将它替换成我们自己的val
  47. * 不然就直接添加好了.
  48. */
  49. if (url.match(pattern)) {
  50. const tmp = new RegExp(`(${key}=)([^&]*)`, 'gi')
  51. url = url.replace(tmp, targetStr)
  52. } else {
  53. const seperator = url.match('[?]') ? '&' : '?'
  54. url = url + seperator + targetStr
  55. }
  56. }
  57. if (location.hash) {
  58. url += location.hash
  59. }
  60. return url
  61. }
  62. /*
  63. * function: _updateHistoryState
  64. * var _prefix = this.options.addrPrefix || ''
  65. * var table = this
  66. * _updateHistoryState( table,_prefix)
  67. * returns void
  68. */
  69. function _updateHistoryState (table, _prefix) {
  70. const params = {}
  71. params[`${_prefix}page`] = table.options.pageNumber
  72. params[`${_prefix}size`] = table.options.pageSize
  73. params[`${_prefix}order`] = table.options.sortOrder
  74. params[`${_prefix}sort`] = table.options.sortName
  75. params[`${_prefix}search`] = table.options.searchText
  76. window.history.pushState({}, '', _buildUrl(params))
  77. }
  78. $.extend($.fn.bootstrapTable.defaults, {
  79. addrbar: false,
  80. addrPrefix: ''
  81. })
  82. $.BootstrapTable = class extends $.BootstrapTable {
  83. init (...args) {
  84. if (
  85. this.options.pagination &&
  86. this.options.addrbar
  87. ) {
  88. // 标志位, 初始加载后关闭
  89. this.addrbarInit = true
  90. this.options.pageNumber = +this.getDefaultOptionValue('pageNumber', 'page')
  91. this.options.pageSize = +this.getDefaultOptionValue('pageSize', 'size')
  92. this.options.sortOrder = this.getDefaultOptionValue('sortOrder', 'order')
  93. this.options.sortName = this.getDefaultOptionValue('sortName', 'sort')
  94. this.options.searchText = this.getDefaultOptionValue('searchText', 'search')
  95. const _prefix = this.options.addrPrefix || ''
  96. const _onLoadSuccess = this.options.onLoadSuccess
  97. const _onPageChange = this.options.onPageChange
  98. this.options.onLoadSuccess = data => {
  99. if (this.addrbarInit) {
  100. this.addrbarInit = false
  101. } else {
  102. _updateHistoryState(this, _prefix)
  103. }
  104. if (_onLoadSuccess) {
  105. _onLoadSuccess.call(this, data)
  106. }
  107. }
  108. this.options.onPageChange = (number, size) => {
  109. _updateHistoryState(this, _prefix)
  110. if (_onPageChange) {
  111. _onPageChange.call(this, number, size)
  112. }
  113. }
  114. }
  115. super.init(...args)
  116. }
  117. /*
  118. * Priority order:
  119. * The value specified by the user has the highest priority.
  120. * If it is not specified, it will be obtained from the address bar.
  121. * If it is not obtained, the default value will be used.
  122. */
  123. getDefaultOptionValue (optionName, prefixName) {
  124. if (this.options[optionName] !== $.BootstrapTable.DEFAULTS[optionName]) {
  125. return this.options[optionName]
  126. }
  127. return _GET(`${this.options.addrPrefix || ''}${prefixName}`) ||
  128. $.BootstrapTable.DEFAULTS[optionName]
  129. }
  130. }