UNPKG

8.07 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.bootstrapTableGroupBy = mod.exports;
12 }
13})(this, function () {
14 'use strict';
15
16 /**
17 * @author: Yura Knoxville
18 * @version: v1.1.0
19 */
20
21 (function ($) {
22
23 'use strict';
24
25 var initBodyCaller, tableGroups;
26
27 // it only does '%s', and return '' when arguments are undefined
28 var sprintf = function sprintf(str) {
29 var args = arguments,
30 flag = true,
31 i = 1;
32
33 str = str.replace(/%s/g, function () {
34 var arg = args[i++];
35
36 if (typeof arg === 'undefined') {
37 flag = false;
38 return '';
39 }
40 return arg;
41 });
42 return flag ? str : '';
43 };
44
45 var groupBy = function groupBy(array, f) {
46 var groups = {};
47 array.forEach(function (o) {
48 var group = f(o);
49 groups[group] = groups[group] || [];
50 groups[group].push(o);
51 });
52
53 return groups;
54 };
55
56 $.extend($.fn.bootstrapTable.defaults, {
57 groupBy: false,
58 groupByField: '',
59 groupByFormatter: undefined
60 });
61
62 var BootstrapTable = $.fn.bootstrapTable.Constructor,
63 _initSort = BootstrapTable.prototype.initSort,
64 _initBody = BootstrapTable.prototype.initBody,
65 _updateSelected = BootstrapTable.prototype.updateSelected;
66
67 BootstrapTable.prototype.initSort = function () {
68 _initSort.apply(this, Array.prototype.slice.apply(arguments));
69
70 var that = this;
71 tableGroups = [];
72
73 if (this.options.groupBy && this.options.groupByField !== '') {
74
75 if (this.options.sortName != this.options.groupByField) {
76 this.data.sort(function (a, b) {
77 return a[that.options.groupByField].localeCompare(b[that.options.groupByField]);
78 });
79 }
80
81 var that = this;
82 var groups = groupBy(that.data, function (item) {
83 return [item[that.options.groupByField]];
84 });
85
86 var index = 0;
87 $.each(groups, function (key, value) {
88 tableGroups.push({
89 id: index,
90 name: key,
91 data: value
92 });
93
94 value.forEach(function (item) {
95 if (!item._data) {
96 item._data = {};
97 }
98
99 item._data['parent-index'] = index;
100 });
101
102 index++;
103 });
104 }
105 };
106
107 BootstrapTable.prototype.initBody = function () {
108 initBodyCaller = true;
109
110 _initBody.apply(this, Array.prototype.slice.apply(arguments));
111
112 if (this.options.groupBy && this.options.groupByField !== '') {
113 var that = this,
114 checkBox = false,
115 visibleColumns = 0;
116
117 this.columns.forEach(function (column) {
118 if (column.checkbox) {
119 checkBox = true;
120 } else {
121 if (column.visible) {
122 visibleColumns += 1;
123 }
124 }
125 });
126
127 if (this.options.detailView && !this.options.cardView) {
128 visibleColumns += 1;
129 }
130
131 tableGroups.forEach(function (item) {
132 var html = [];
133
134 html.push(sprintf('<tr class="info groupBy expanded" data-group-index="%s">', item.id));
135
136 if (that.options.detailView && !that.options.cardView) {
137 html.push('<td class="detail"></td>');
138 }
139
140 if (checkBox) {
141 html.push('<td class="bs-checkbox">', '<input name="btSelectGroup" type="checkbox" />', '</td>');
142 }
143 var formattedValue = item.name;
144 if (typeof that.options.groupByFormatter == "function") {
145 formattedValue = that.options.groupByFormatter(item.name, item.id, item.data);
146 }
147 html.push('<td', sprintf(' colspan="%s"', visibleColumns), '>', formattedValue, '</td>');
148
149 html.push('</tr>');
150
151 that.$body.find('tr[data-parent-index=' + item.id + ']:first').before($(html.join('')));
152 });
153
154 this.$selectGroup = [];
155 this.$body.find('[name="btSelectGroup"]').each(function () {
156 var self = $(this);
157
158 that.$selectGroup.push({
159 group: self,
160 item: that.$selectItem.filter(function () {
161 return $(this).closest('tr').data('parent-index') === self.closest('tr').data('group-index');
162 })
163 });
164 });
165
166 this.$container.off('click', '.groupBy').on('click', '.groupBy', function () {
167 $(this).toggleClass('expanded');
168 that.$body.find('tr[data-parent-index=' + $(this).closest('tr').data('group-index') + ']').toggleClass('hidden');
169 });
170
171 this.$container.off('click', '[name="btSelectGroup"]').on('click', '[name="btSelectGroup"]', function (event) {
172 event.stopImmediatePropagation();
173
174 var self = $(this);
175 var checked = self.prop('checked');
176 that[checked ? 'checkGroup' : 'uncheckGroup']($(this).closest('tr').data('group-index'));
177 });
178 }
179
180 initBodyCaller = false;
181 this.updateSelected();
182 };
183
184 BootstrapTable.prototype.updateSelected = function () {
185 if (!initBodyCaller) {
186 _updateSelected.apply(this, Array.prototype.slice.apply(arguments));
187
188 if (this.options.groupBy && this.options.groupByField !== '') {
189 this.$selectGroup.forEach(function (item) {
190 var checkGroup = item.item.filter(':enabled').length === item.item.filter(':enabled').filter(':checked').length;
191
192 item.group.prop('checked', checkGroup);
193 });
194 }
195 }
196 };
197
198 BootstrapTable.prototype.getGroupSelections = function (index) {
199 var that = this;
200
201 return $.grep(this.data, function (row) {
202 return row[that.header.stateField] && row._data['parent-index'] === index;
203 });
204 };
205
206 BootstrapTable.prototype.checkGroup = function (index) {
207 this.checkGroup_(index, true);
208 };
209
210 BootstrapTable.prototype.uncheckGroup = function (index) {
211 this.checkGroup_(index, false);
212 };
213
214 BootstrapTable.prototype.checkGroup_ = function (index, checked) {
215 var rows;
216 var filter = function filter() {
217 return $(this).closest('tr').data('parent-index') === index;
218 };
219
220 if (!checked) {
221 rows = this.getGroupSelections(index);
222 }
223
224 this.$selectItem.filter(filter).prop('checked', checked);
225
226 this.updateRows();
227 this.updateSelected();
228 if (checked) {
229 rows = this.getGroupSelections(index);
230 }
231 this.trigger(checked ? 'check-all' : 'uncheck-all', rows);
232 };
233 })(jQuery);
234});
\No newline at end of file