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 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | !function ($) {
|
23 |
|
24 | 'use strict';
|
25 |
|
26 | var originalRowAttr,
|
27 | dataTTId = 'data-tt-id',
|
28 | dataTTParentId = 'data-tt-parent-id',
|
29 | obj = {},
|
30 | parentId = undefined;
|
31 |
|
32 | var getParentRowId = function getParentRowId(that, id) {
|
33 | var parentRows = that.$body.find('tr').not('[' + 'data-tt-parent-id]');
|
34 |
|
35 | for (var i = 0; i < parentRows.length; i++) {
|
36 | if (i === id) {
|
37 | return $(parentRows[i]).attr('data-tt-id');
|
38 | }
|
39 | }
|
40 |
|
41 | return undefined;
|
42 | };
|
43 |
|
44 | var sumData = function sumData(that, data) {
|
45 | var sumRow = {};
|
46 | $.each(data, function (i, row) {
|
47 | if (!row.IsParent) {
|
48 | for (var prop in row) {
|
49 | if (!isNaN(parseFloat(row[prop]))) {
|
50 | if (that.columns[that.fieldsColumnsIndex[prop]].groupBySumGroup) {
|
51 | if (sumRow[prop] === undefined) {
|
52 | sumRow[prop] = 0;
|
53 | }
|
54 | sumRow[prop] += +row[prop];
|
55 | }
|
56 | }
|
57 | }
|
58 | }
|
59 | });
|
60 | return sumRow;
|
61 | };
|
62 |
|
63 | var rowAttr = function rowAttr(row, index) {
|
64 |
|
65 | originalRowAttr.apply([row, index]);
|
66 |
|
67 | obj[dataTTId.toString()] = index;
|
68 |
|
69 | if (!row.IsParent) {
|
70 | obj[dataTTParentId.toString()] = parentId === undefined ? index : parentId;
|
71 | } else {
|
72 | parentId = index;
|
73 | delete obj[dataTTParentId.toString()];
|
74 | }
|
75 |
|
76 | return obj;
|
77 | };
|
78 |
|
79 | var setObjectKeys = function setObjectKeys() {
|
80 |
|
81 | Object.keys = function (o) {
|
82 | if (o !== Object(o)) {
|
83 | throw new TypeError('Object.keys called on a non-object');
|
84 | }
|
85 | var k = [],
|
86 | p;
|
87 | for (p in o) {
|
88 | if (Object.prototype.hasOwnProperty.call(o, p)) {
|
89 | k.push(p);
|
90 | }
|
91 | }
|
92 | return k;
|
93 | };
|
94 | };
|
95 |
|
96 | var getDataArrayFromItem = function getDataArrayFromItem(that, item) {
|
97 | var itemDataArray = [];
|
98 | for (var i = 0; i < that.options.groupByField.length; i++) {
|
99 | itemDataArray.push(item[that.options.groupByField[i]]);
|
100 | }
|
101 |
|
102 | return itemDataArray;
|
103 | };
|
104 |
|
105 | var getNewRow = function getNewRow(that, result, index) {
|
106 | var newRow = {};
|
107 | for (var i = 0; i < that.options.groupByField.length; i++) {
|
108 | newRow[that.options.groupByField[i].toString()] = result[index][0][that.options.groupByField[i]];
|
109 | }
|
110 |
|
111 | newRow.IsParent = true;
|
112 |
|
113 | return newRow;
|
114 | };
|
115 |
|
116 | var groupBy = function groupBy(array, f) {
|
117 | var groups = {};
|
118 | $.each(array, function (i, o) {
|
119 | var group = JSON.stringify(f(o));
|
120 | groups[group] = groups[group] || [];
|
121 | groups[group].push(o);
|
122 | });
|
123 | return Object.keys(groups).map(function (group) {
|
124 | return groups[group];
|
125 | });
|
126 | };
|
127 |
|
128 | var makeGrouped = function makeGrouped(that, data) {
|
129 | var newData = [],
|
130 | sumRow = {};
|
131 |
|
132 | var result = groupBy(data, function (item) {
|
133 | return getDataArrayFromItem(that, item);
|
134 | });
|
135 |
|
136 | for (var i = 0; i < result.length; i++) {
|
137 | result[i].unshift(getNewRow(that, result, i));
|
138 | if (that.options.groupBySumGroup) {
|
139 | sumRow = sumData(that, result[i]);
|
140 | if (!$.isEmptyObject(sumRow)) {
|
141 | result[i].push(sumRow);
|
142 | }
|
143 | }
|
144 | }
|
145 |
|
146 | newData = newData.concat.apply(newData, result);
|
147 |
|
148 | if (!that.options.loaded && newData.length > 0) {
|
149 | that.options.loaded = true;
|
150 | that.options.originalData = that.options.data;
|
151 | that.options.data = newData;
|
152 | }
|
153 |
|
154 | return newData;
|
155 | };
|
156 |
|
157 | $.extend($.fn.bootstrapTable.defaults, {
|
158 | groupBy: false,
|
159 | groupByField: [],
|
160 | groupBySumGroup: false,
|
161 | groupByInitExpanded: undefined,
|
162 |
|
163 | loaded: false,
|
164 | originalData: undefined
|
165 | });
|
166 |
|
167 | $.fn.bootstrapTable.methods.push('collapseAll', 'expandAll', 'refreshGroupByField');
|
168 |
|
169 | $.extend($.fn.bootstrapTable.COLUMN_DEFAULTS, {
|
170 | groupBySumGroup: false
|
171 | });
|
172 |
|
173 | var BootstrapTable = $.fn.bootstrapTable.Constructor,
|
174 | _init = BootstrapTable.prototype.init,
|
175 | _initData = BootstrapTable.prototype.initData;
|
176 |
|
177 | BootstrapTable.prototype.init = function () {
|
178 |
|
179 | if (!this.options.sortName) {
|
180 | if (this.options.groupBy && this.options.groupByField.length > 0) {
|
181 | var that = this;
|
182 |
|
183 |
|
184 | if (!Object.keys) {
|
185 | $.fn.bootstrapTable.utils.objectKeys();
|
186 | }
|
187 |
|
188 |
|
189 | this.options.loaded = false;
|
190 | this.options.originalData = undefined;
|
191 |
|
192 | originalRowAttr = this.options.rowAttributes;
|
193 | this.options.rowAttributes = rowAttr;
|
194 | this.$el.off('post-body.bs.table').on('post-body.bs.table', function () {
|
195 | that.$el.treetable({
|
196 | expandable: true,
|
197 | onNodeExpand: function onNodeExpand() {
|
198 | if (that.options.height) {
|
199 | that.resetHeader();
|
200 | }
|
201 | },
|
202 | onNodeCollapse: function onNodeCollapse() {
|
203 | if (that.options.height) {
|
204 | that.resetHeader();
|
205 | }
|
206 | }
|
207 | }, true);
|
208 |
|
209 | if (that.options.groupByInitExpanded !== undefined) {
|
210 | if (typeof that.options.groupByInitExpanded === 'number') {
|
211 | that.expandNode(that.options.groupByInitExpanded);
|
212 | } else if (that.options.groupByInitExpanded.toLowerCase() === 'all') {
|
213 | that.expandAll();
|
214 | }
|
215 | }
|
216 | });
|
217 | }
|
218 | }
|
219 | _init.apply(this, Array.prototype.slice.apply(arguments));
|
220 | };
|
221 |
|
222 | BootstrapTable.prototype.initData = function (data, type) {
|
223 |
|
224 | if (!this.options.sortName) {
|
225 | if (this.options.groupBy && this.options.groupByField.length > 0) {
|
226 |
|
227 | this.options.groupByField = typeof this.options.groupByField === 'string' ? this.options.groupByField.replace('[', '').replace(']', '').replace(/ /g, '').toLowerCase().split(',') : this.options.groupByField;
|
228 |
|
229 | data = makeGrouped(this, data ? data : this.options.data);
|
230 | }
|
231 | }
|
232 | _initData.apply(this, [data, type]);
|
233 | };
|
234 |
|
235 | BootstrapTable.prototype.expandAll = function () {
|
236 | this.$el.treetable('expandAll');
|
237 | };
|
238 |
|
239 | BootstrapTable.prototype.collapseAll = function () {
|
240 | this.$el.treetable('collapseAll');
|
241 | };
|
242 |
|
243 | BootstrapTable.prototype.expandNode = function (id) {
|
244 | id = getParentRowId(this, id);
|
245 | if (id !== undefined) {
|
246 | this.$el.treetable('expandNode', id);
|
247 | }
|
248 | };
|
249 |
|
250 | BootstrapTable.prototype.refreshGroupByField = function (groupByFields) {
|
251 | if (!$.fn.bootstrapTable.utils.compareObjects(this.options.groupByField, groupByFields)) {
|
252 | this.options.groupByField = groupByFields;
|
253 | this.load(this.options.originalData);
|
254 | }
|
255 | };
|
256 | }(jQuery);
|
257 | }); |
\ | No newline at end of file |