bootstrap-table-auto-refresh.js 2.7 KB

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