UNPKG

3.79 kBJavaScriptView Raw
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 /**
17 * @author Homer Glascock <HopGlascock@gmail.com>
18 * @version: v1.0.0
19 */
20
21 !function ($) {
22 "use strict";
23
24 var sprintf = $.fn.bootstrapTable.utils.sprintf;
25
26 var reInit = function reInit(self) {
27 self.initHeader();
28 self.initSearch();
29 self.initPagination();
30 self.initBody();
31 };
32
33 $.extend($.fn.bootstrapTable.defaults, {
34 showToggleBtn: false,
35 multiToggleDefaults: [] //column names go here
36 });
37
38 $.fn.bootstrapTable.methods.push('hideAllColumns', 'showAllColumns');
39
40 var BootstrapTable = $.fn.bootstrapTable.Constructor,
41 _initToolbar = BootstrapTable.prototype.initToolbar;
42
43 BootstrapTable.prototype.initToolbar = function () {
44
45 _initToolbar.apply(this, Array.prototype.slice.apply(arguments));
46
47 var that = this,
48 $btnGroup = this.$toolbar.find('>.btn-group');
49
50 if (typeof this.options.multiToggleDefaults === 'string') {
51 this.options.multiToggleDefaults = JSON.parse(this.options.multiToggleDefaults);
52 }
53
54 if (this.options.showToggleBtn && this.options.showColumns) {
55 var showbtn = "<button class='btn btn-default hidden' id='showAllBtn'><span class='glyphicon glyphicon-resize-full icon-zoom-in'></span></button>",
56 hidebtn = "<button class='btn btn-default' id='hideAllBtn'><span class='glyphicon glyphicon-resize-small icon-zoom-out'></span></button>";
57
58 $btnGroup.append(showbtn + hidebtn);
59
60 $btnGroup.find('#showAllBtn').click(function () {
61 that.showAllColumns();
62 $btnGroup.find('#hideAllBtn').toggleClass('hidden');
63 $btnGroup.find('#showAllBtn').toggleClass('hidden');
64 });
65 $btnGroup.find('#hideAllBtn').click(function () {
66 that.hideAllColumns();
67 $btnGroup.find('#hideAllBtn').toggleClass('hidden');
68 $btnGroup.find('#showAllBtn').toggleClass('hidden');
69 });
70 }
71 };
72
73 BootstrapTable.prototype.hideAllColumns = function () {
74 var that = this,
75 defaults = that.options.multiToggleDefaults;
76
77 $.each(this.columns, function (index, column) {
78 //if its one of the defaults dont touch it
79 if (defaults.indexOf(column.field) == -1 && column.switchable) {
80 column.visible = false;
81 var $items = that.$toolbar.find('.keep-open input').prop('disabled', false);
82 $items.filter(sprintf('[value="%s"]', index)).prop('checked', false);
83 }
84 });
85
86 reInit(that);
87 };
88
89 BootstrapTable.prototype.showAllColumns = function () {
90 var that = this;
91 $.each(this.columns, function (index, column) {
92 if (column.switchable) {
93 column.visible = true;
94 }
95
96 var $items = that.$toolbar.find('.keep-open input').prop('disabled', false);
97 $items.filter(sprintf('[value="%s"]', index)).prop('checked', true);
98 });
99
100 reInit(that);
101
102 that.toggleColumn(0, that.columns[0].visible, false);
103 };
104 }(jQuery);
105});
\No newline at end of file