bootstrap-table-multi-toggle.js 3.1 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 () {
  33. that.showAllColumns();
  34. $btnGroup.find('#hideAllBtn').toggleClass('hidden');
  35. $btnGroup.find('#showAllBtn').toggleClass('hidden');
  36. });
  37. $btnGroup.find('#hideAllBtn').click(function () {
  38. that.hideAllColumns();
  39. $btnGroup.find('#hideAllBtn').toggleClass('hidden');
  40. $btnGroup.find('#showAllBtn').toggleClass('hidden');
  41. });
  42. }
  43. };
  44. BootstrapTable.prototype.hideAllColumns = function () {
  45. var that = this,
  46. defaults = that.options.multiToggleDefaults;
  47. $.each(this.columns, function (index, column) {
  48. //if its one of the defaults dont touch it
  49. if (defaults.indexOf(column.field) == -1 && column.switchable) {
  50. column.visible = false;
  51. var $items = that.$toolbar.find('.keep-open input').prop('disabled', false);
  52. $items.filter(sprintf('[value="%s"]', index)).prop('checked', false);
  53. }
  54. });
  55. reInit(that);
  56. };
  57. BootstrapTable.prototype.showAllColumns = function () {
  58. var that = this;
  59. $.each(this.columns, function (index, column) {
  60. if (column.switchable) {
  61. column.visible = true;
  62. }
  63. var $items = that.$toolbar.find('.keep-open input').prop('disabled', false);
  64. $items.filter(sprintf('[value="%s"]', index)).prop('checked', true);
  65. });
  66. reInit(that);
  67. that.toggleColumn(0, that.columns[0].visible, false);
  68. };
  69. }(jQuery);