bootstrap-table-multi-toggle.js 3.0 KB

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