bootstrap-table-auto-refresh.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /**
  2. * @author: Alec Fenichel
  3. * @webSite: https://fenichelar.com
  4. * @update: zhixin wen <wenzhixin2010@gmail.com>
  5. */
  6. const Utils = $.fn.bootstrapTable.utils
  7. Object.assign($.fn.bootstrapTable.defaults, {
  8. autoRefresh: false,
  9. showAutoRefresh: true,
  10. autoRefreshInterval: 60,
  11. autoRefreshSilent: true,
  12. autoRefreshStatus: true,
  13. autoRefreshFunction: null
  14. })
  15. Object.assign($.fn.bootstrapTable.defaults.icons, {
  16. autoRefresh: {
  17. bootstrap3: 'glyphicon-time icon-time',
  18. bootstrap5: 'bi-clock',
  19. materialize: 'access_time',
  20. 'bootstrap-table': 'icon-clock'
  21. }[$.fn.bootstrapTable.theme] || 'fa-clock'
  22. })
  23. Object.assign($.fn.bootstrapTable.locales, {
  24. formatAutoRefresh () {
  25. return 'Auto Refresh'
  26. }
  27. })
  28. Object.assign($.fn.bootstrapTable.defaults, $.fn.bootstrapTable.locales)
  29. $.BootstrapTable = class extends $.BootstrapTable {
  30. init (...args) {
  31. super.init(...args)
  32. if (this.options.autoRefresh && this.options.autoRefreshStatus) {
  33. this.setupRefreshInterval()
  34. }
  35. }
  36. initToolbar (...args) {
  37. if (this.options.autoRefresh) {
  38. this.buttons = Object.assign(this.buttons, {
  39. autoRefresh: {
  40. html: `
  41. <button class="auto-refresh ${this.constants.buttonsClass}
  42. ${this.options.autoRefreshStatus ? ` ${this.constants.classes.buttonActive}` : ''}"
  43. type="button" name="autoRefresh" title="${this.options.formatAutoRefresh()}">
  44. ${this.options.showButtonIcons ? Utils.sprintf(this.constants.html.icon, this.options.iconsPrefix, this.options.icons.autoRefresh) : ''}
  45. ${this.options.showButtonText ? this.options.formatAutoRefresh() : ''}
  46. </button>
  47. `,
  48. event: this.toggleAutoRefresh
  49. }
  50. })
  51. }
  52. super.initToolbar(...args)
  53. }
  54. toggleAutoRefresh () {
  55. if (this.options.autoRefresh) {
  56. if (this.options.autoRefreshStatus) {
  57. clearInterval(this.options.autoRefreshFunction)
  58. this.$toolbar.find('>.columns .auto-refresh')
  59. .removeClass(this.constants.classes.buttonActive)
  60. } else {
  61. this.setupRefreshInterval()
  62. this.$toolbar.find('>.columns .auto-refresh')
  63. .addClass(this.constants.classes.buttonActive)
  64. }
  65. this.options.autoRefreshStatus = !this.options.autoRefreshStatus
  66. }
  67. }
  68. destroy () {
  69. if (this.options.autoRefresh && this.options.autoRefreshStatus) {
  70. clearInterval(this.options.autoRefreshFunction)
  71. }
  72. super.destroy()
  73. }
  74. setupRefreshInterval () {
  75. this.options.autoRefreshFunction = setInterval(() => {
  76. if (!this.options.autoRefresh || !this.options.autoRefreshStatus) {
  77. return
  78. }
  79. this.refresh({ silent: this.options.autoRefreshSilent })
  80. }, this.options.autoRefreshInterval * 1000)
  81. }
  82. }