bootstrap-table-auto-refresh.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /**
  2. * @author: Alec Fenichel
  3. * @webSite: https://fenichelar.com
  4. * @version: v1.0.0
  5. */
  6. (function ($) {
  7. 'use strict';
  8. $.extend($.fn.bootstrapTable.defaults, {
  9. autoRefresh: false,
  10. autoRefreshInterval: 60,
  11. autoRefreshSilent: true,
  12. autoRefreshStatus: true,
  13. autoRefreshFunction: null
  14. });
  15. $.extend($.fn.bootstrapTable.defaults.icons, {
  16. autoRefresh: 'glyphicon-time icon-time'
  17. });
  18. $.extend($.fn.bootstrapTable.locales, {
  19. formatAutoRefresh: function() {
  20. return 'Auto Refresh';
  21. }
  22. });
  23. $.extend($.fn.bootstrapTable.defaults, $.fn.bootstrapTable.locales);
  24. var BootstrapTable = $.fn.bootstrapTable.Constructor;
  25. var _init = BootstrapTable.prototype.init;
  26. var _initToolbar = BootstrapTable.prototype.initToolbar;
  27. var sprintf = $.fn.bootstrapTable.utils.sprintf;
  28. BootstrapTable.prototype.init = function () {
  29. _init.apply(this, Array.prototype.slice.apply(arguments));
  30. if (this.options.autoRefresh && this.options.autoRefreshStatus) {
  31. var that = this;
  32. this.options.autoRefreshFunction = setInterval(function () {
  33. that.refresh({silent: that.options.autoRefreshSilent});
  34. }, this.options.autoRefreshInterval*1000);
  35. }
  36. };
  37. BootstrapTable.prototype.initToolbar = function() {
  38. _initToolbar.apply(this, Array.prototype.slice.apply(arguments));
  39. if (this.options.autoRefresh) {
  40. var $btnGroup = this.$toolbar.find('>.btn-group');
  41. var $btnAutoRefresh = $btnGroup.find('.auto-refresh');
  42. if (!$btnAutoRefresh.length) {
  43. $btnAutoRefresh = $([
  44. sprintf('<button class="btn btn-default auto-refresh %s" ', this.options.autoRefreshStatus ? 'enabled' : ''),
  45. 'type="button" ',
  46. sprintf('title="%s">', this.options.formatAutoRefresh()),
  47. sprintf('<i class="%s %s"></i>', this.options.iconsPrefix, this.options.icons.autoRefresh),
  48. '</button>'
  49. ].join('')).appendTo($btnGroup);
  50. $btnAutoRefresh.on('click', $.proxy(this.toggleAutoRefresh, this));
  51. }
  52. }
  53. };
  54. BootstrapTable.prototype.toggleAutoRefresh = function() {
  55. if (this.options.autoRefresh) {
  56. if (this.options.autoRefreshStatus) {
  57. clearInterval(this.options.autoRefreshFunction);
  58. this.$toolbar.find('>.btn-group').find('.auto-refresh').removeClass('enabled');
  59. } else {
  60. var that = this;
  61. this.options.autoRefreshFunction = setInterval(function () {
  62. that.refresh({silent: that.options.autoRefreshSilent});
  63. }, this.options.autoRefreshInterval*1000);
  64. this.$toolbar.find('>.btn-group').find('.auto-refresh').addClass('enabled');
  65. }
  66. this.options.autoRefreshStatus = !this.options.autoRefreshStatus;
  67. }
  68. };
  69. })(jQuery);