bootstrap-table-addrbar.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. Object.assign($.fn.bootstrapTable.defaults, {
  9. addrbar: false,
  10. addrPrefix: ''
  11. })
  12. $.BootstrapTable = class extends $.BootstrapTable {
  13. init (...args) {
  14. if (
  15. this.options.pagination &&
  16. this.options.addrbar
  17. ) {
  18. this.initAddrbar()
  19. }
  20. super.init(...args)
  21. }
  22. /*
  23. * Priority order:
  24. * The value specified by the user has the highest priority.
  25. * If it is not specified, it will be obtained from the address bar.
  26. * If it is not obtained, the default value will be used.
  27. */
  28. getDefaultOptionValue (optionName, prefixName) {
  29. if (this.options[optionName] !== $.BootstrapTable.DEFAULTS[optionName]) {
  30. return this.options[optionName]
  31. }
  32. return this.searchParams.get(`${this.options.addrPrefix || ''}${prefixName}`) ||
  33. $.BootstrapTable.DEFAULTS[optionName]
  34. }
  35. initAddrbar () {
  36. // 标志位, 初始加载后关闭
  37. this.addrbarInit = true
  38. this.searchParams = new URLSearchParams(window.location.search.substring(1))
  39. this.options.pageNumber = +this.getDefaultOptionValue('pageNumber', 'page')
  40. this.options.pageSize = +this.getDefaultOptionValue('pageSize', 'size')
  41. this.options.sortOrder = this.getDefaultOptionValue('sortOrder', 'order')
  42. this.options.sortName = this.getDefaultOptionValue('sortName', 'sort')
  43. this.options.searchText = this.getDefaultOptionValue('searchText', 'search')
  44. const prefix = this.options.addrPrefix || ''
  45. const onLoadSuccess = this.options.onLoadSuccess
  46. const onPageChange = this.options.onPageChange
  47. this.options.onLoadSuccess = data => {
  48. if (this.addrbarInit) {
  49. this.addrbarInit = false
  50. } else {
  51. this.updateHistoryState(prefix)
  52. }
  53. if (onLoadSuccess) {
  54. onLoadSuccess.call(this, data)
  55. }
  56. }
  57. this.options.onPageChange = (number, size) => {
  58. this.updateHistoryState(prefix)
  59. if (onPageChange) {
  60. onPageChange.call(this, number, size)
  61. }
  62. }
  63. }
  64. updateHistoryState (prefix) {
  65. const params = {}
  66. params[`${prefix}page`] = this.options.pageNumber
  67. params[`${prefix}size`] = this.options.pageSize
  68. params[`${prefix}order`] = this.options.sortOrder
  69. params[`${prefix}sort`] = this.options.sortName
  70. params[`${prefix}search`] = this.options.searchText
  71. for (const [key, value] of Object.entries(params)) {
  72. if (value === undefined) {
  73. this.searchParams.delete(key)
  74. } else {
  75. this.searchParams.set(key, value)
  76. }
  77. }
  78. let url = `?${this.searchParams.toString()}`
  79. if (location.hash) {
  80. url += location.hash
  81. }
  82. window.history.pushState({}, '', url)
  83. }
  84. resetSearch (text) {
  85. super.resetSearch(text)
  86. this.options.searchText = text || ''
  87. }
  88. }