bootstrap-table-multi-toggle.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. (function (global, factory) {
  2. if (typeof define === "function" && define.amd) {
  3. define([], factory);
  4. } else if (typeof exports !== "undefined") {
  5. factory();
  6. } else {
  7. var mod = {
  8. exports: {}
  9. };
  10. factory();
  11. global.bootstrapTableMultiToggle = mod.exports;
  12. }
  13. })(this, function () {
  14. 'use strict';
  15. /**
  16. * @author Homer Glascock <HopGlascock@gmail.com>
  17. * @version: v1.0.0
  18. */
  19. !function ($) {
  20. "use strict";
  21. var sprintf = $.fn.bootstrapTable.utils.sprintf;
  22. var reInit = function reInit(self) {
  23. self.initHeader();
  24. self.initSearch();
  25. self.initPagination();
  26. self.initBody();
  27. };
  28. $.extend($.fn.bootstrapTable.defaults, {
  29. showToggleBtn: false,
  30. multiToggleDefaults: [] //column names go here
  31. });
  32. $.fn.bootstrapTable.methods.push('hideAllColumns', 'showAllColumns');
  33. var BootstrapTable = $.fn.bootstrapTable.Constructor,
  34. _initToolbar = BootstrapTable.prototype.initToolbar;
  35. BootstrapTable.prototype.initToolbar = function () {
  36. _initToolbar.apply(this, Array.prototype.slice.apply(arguments));
  37. var that = this,
  38. $btnGroup = this.$toolbar.find('>.btn-group');
  39. if (typeof this.options.multiToggleDefaults === 'string') {
  40. this.options.multiToggleDefaults = JSON.parse(this.options.multiToggleDefaults);
  41. }
  42. if (this.options.showToggleBtn && this.options.showColumns) {
  43. var showbtn = "<button class='btn btn-default hidden' id='showAllBtn'><span class='glyphicon glyphicon-resize-full icon-zoom-in'></span></button>",
  44. hidebtn = "<button class='btn btn-default' id='hideAllBtn'><span class='glyphicon glyphicon-resize-small icon-zoom-out'></span></button>";
  45. $btnGroup.append(showbtn + hidebtn);
  46. $btnGroup.find('#showAllBtn').click(function () {
  47. that.showAllColumns();
  48. $btnGroup.find('#hideAllBtn').toggleClass('hidden');
  49. $btnGroup.find('#showAllBtn').toggleClass('hidden');
  50. });
  51. $btnGroup.find('#hideAllBtn').click(function () {
  52. that.hideAllColumns();
  53. $btnGroup.find('#hideAllBtn').toggleClass('hidden');
  54. $btnGroup.find('#showAllBtn').toggleClass('hidden');
  55. });
  56. }
  57. };
  58. BootstrapTable.prototype.hideAllColumns = function () {
  59. var that = this,
  60. defaults = that.options.multiToggleDefaults;
  61. $.each(this.columns, function (index, column) {
  62. //if its one of the defaults dont touch it
  63. if (defaults.indexOf(column.field) == -1 && column.switchable) {
  64. column.visible = false;
  65. var $items = that.$toolbar.find('.keep-open input').prop('disabled', false);
  66. $items.filter(sprintf('[value="%s"]', index)).prop('checked', false);
  67. }
  68. });
  69. reInit(that);
  70. };
  71. BootstrapTable.prototype.showAllColumns = function () {
  72. var that = this;
  73. $.each(this.columns, function (index, column) {
  74. if (column.switchable) {
  75. column.visible = true;
  76. }
  77. var $items = that.$toolbar.find('.keep-open input').prop('disabled', false);
  78. $items.filter(sprintf('[value="%s"]', index)).prop('checked', true);
  79. });
  80. reInit(that);
  81. that.toggleColumn(0, that.columns[0].visible, false);
  82. };
  83. }(jQuery);
  84. });