|
1 | 1 | /**
|
2 | 2 | * @author: Dennis Hernández
|
3 | 3 | * @webSite: http://djhvscf.github.io/Blog
|
4 |
| - * @version: v1.0.0 |
| 4 | + * @version: v1.1.0 |
5 | 5 | */
|
6 | 6 |
|
7 | 7 | !function ($) {
|
|
14 | 14 | obj = {},
|
15 | 15 | parentId = undefined;
|
16 | 16 |
|
| 17 | + var compareObjects = function (objectA, objectB, compareLength) { |
| 18 | + // Create arrays of property names |
| 19 | + var objectAProperties = Object.getOwnPropertyNames(objectA), |
| 20 | + objectBProperties = Object.getOwnPropertyNames(objectB), |
| 21 | + propName = ''; |
| 22 | + |
| 23 | + if (compareLength) { |
| 24 | + // If number of properties is different, objects are not equivalent |
| 25 | + if (objectAProperties.length !== objectBProperties.length) { |
| 26 | + return false; |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + for (var i = 0; i < objectAProperties.length; i++) { |
| 31 | + propName = objectAProperties[i]; |
| 32 | + |
| 33 | + // If the property is not in the object B properties, continue with the next property |
| 34 | + if ($.inArray(propName, objectBProperties) > -1) { |
| 35 | + // If values of same property are not equal, objects are not equivalent |
| 36 | + if (objectA[propName] !== objectB[propName]) { |
| 37 | + return false; |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + // If we made it this far, objects are considered equivalent |
| 43 | + return true; |
| 44 | + }; |
| 45 | + |
17 | 46 | var getFieldIndex = function (columns, field) {
|
18 | 47 | var index = -1;
|
19 | 48 |
|
|
42 | 71 | var sumData = function (that, data) {
|
43 | 72 | var sumRow = {};
|
44 | 73 | $.each(data, function (i, row) {
|
45 |
| - for(var prop in row) { |
46 |
| - if (!row.IsParent) { |
47 |
| - if (!isNaN(parseFloat(row[prop]))) { |
48 |
| - if (that.columns[getFieldIndex(that.columns, prop)].groupBySumGroup) { |
49 |
| - if (sumRow[prop] === undefined) { |
50 |
| - sumRow[prop] = 0; |
| 74 | + if (!row.IsParent) { |
| 75 | + for(var prop in row) { |
| 76 | + if (!isNaN(parseFloat(row[prop]))) { |
| 77 | + if (that.columns[getFieldIndex(that.columns, prop)].groupBySumGroup) { |
| 78 | + if (sumRow[prop] === undefined) { |
| 79 | + sumRow[prop] = 0; |
| 80 | + } |
| 81 | + sumRow[prop] += +row[prop]; |
51 | 82 | }
|
52 |
| - sumRow[prop] += +row[prop]; |
53 | 83 | }
|
54 |
| - } |
55 | 84 | }
|
56 | 85 | }
|
57 | 86 | });
|
|
91 | 120 | }
|
92 | 121 | };
|
93 | 122 |
|
| 123 | + var getDataArrayFromItem = function (that, item) { |
| 124 | + var itemDataArray = []; |
| 125 | + for (var i = 0; i < that.options.groupByField.length; i++) { |
| 126 | + itemDataArray.push(item[that.options.groupByField[i]]); |
| 127 | + } |
| 128 | + |
| 129 | + return itemDataArray; |
| 130 | + }; |
| 131 | + |
| 132 | + var getNewRow = function (that, result, index) { |
| 133 | + var newRow = {}; |
| 134 | + for (var i = 0; i < that.options.groupByField.length; i++) { |
| 135 | + newRow[that.options.groupByField[i].toString()] = result[index][0][that.options.groupByField[i]]; |
| 136 | + } |
| 137 | + |
| 138 | + newRow.IsParent = true; |
| 139 | + |
| 140 | + return newRow; |
| 141 | + }; |
| 142 | + |
94 | 143 | var groupBy = function (array , f) {
|
95 | 144 | var groups = {};
|
96 | 145 | $.each(array, function(i, o) {
|
|
104 | 153 | };
|
105 | 154 |
|
106 | 155 | var makeGrouped = function (that, data) {
|
107 |
| - var newRow = {}, |
108 |
| - newData = [], |
| 156 | + var newData = [], |
109 | 157 | sumRow = {};
|
110 | 158 |
|
111 | 159 | var result = groupBy(data, function (item) {
|
112 |
| - return [item[that.options.groupByField]]; |
| 160 | + return getDataArrayFromItem(that, item); |
113 | 161 | });
|
114 | 162 |
|
115 | 163 | for (var i = 0; i < result.length; i++) {
|
116 |
| - newRow[that.options.groupByField.toString()] = result[i][0][that.options.groupByField]; |
117 |
| - newRow.IsParent = true; |
118 |
| - result[i].unshift(newRow); |
| 164 | + result[i].unshift(getNewRow(that, result, i)); |
119 | 165 | if (that.options.groupBySumGroup) {
|
120 |
| - sumRow = sumData(that, result[i]) |
| 166 | + sumRow = sumData(that, result[i]); |
121 | 167 | if (!$.isEmptyObject(sumRow)) {
|
122 | 168 | result[i].push(sumRow);
|
123 | 169 | }
|
124 | 170 | }
|
125 |
| - newRow = {}; |
126 | 171 | }
|
127 | 172 |
|
128 | 173 | newData = newData.concat.apply(newData, result);
|
|
138 | 183 |
|
139 | 184 | $.extend($.fn.bootstrapTable.defaults, {
|
140 | 185 | groupBy: false,
|
141 |
| - groupByField: '', |
| 186 | + groupByField: [], |
142 | 187 | groupBySumGroup: false,
|
143 | 188 | groupByInitExpanded: undefined, //node, 'all'
|
144 | 189 | //internal variables
|
145 | 190 | loaded: false,
|
146 | 191 | originalData: undefined
|
147 | 192 | });
|
148 | 193 |
|
149 |
| - $.fn.bootstrapTable.methods.push('collapseAll', 'expandAll'); |
| 194 | + $.fn.bootstrapTable.methods.push('collapseAll', 'expandAll', 'refreshGroupByField'); |
150 | 195 |
|
151 | 196 | $.extend($.fn.bootstrapTable.COLUMN_DEFAULTS, {
|
152 | 197 | groupBySumGroup: false
|
|
159 | 204 | BootstrapTable.prototype.init = function () {
|
160 | 205 | //Temporal validation
|
161 | 206 | if (!this.options.sortName) {
|
162 |
| - if ((this.options.groupBy) && (this.options.groupByField !== '')) { |
| 207 | + if ((this.options.groupBy) && (this.options.groupByField.length > 0)) { |
163 | 208 | var that = this;
|
164 | 209 |
|
165 | 210 | // Compatibility: IE < 9 and old browsers
|
|
204 | 249 | BootstrapTable.prototype.initData = function (data, type) {
|
205 | 250 | //Temporal validation
|
206 | 251 | if (!this.options.sortName) {
|
207 |
| - if ((this.options.groupBy) && (this.options.groupByField !== '')) { |
| 252 | + if ((this.options.groupBy) && (this.options.groupByField.length > 0)) { |
| 253 | + |
| 254 | + this.options.groupByField = typeof this.options.groupByField === 'string' ? |
| 255 | + this.options.groupByField.replace('[', '').replace(']', '') |
| 256 | + .replace(/ /g, '').toLowerCase().split(',') : this.options.groupByField; |
| 257 | + |
208 | 258 | data = makeGrouped(this, data ? data : this.options.data);
|
209 | 259 | }
|
210 | 260 | }
|
|
224 | 274 | if (id !== undefined) {
|
225 | 275 | this.$el.treetable('expandNode', id);
|
226 | 276 | }
|
227 |
| - } |
| 277 | + }; |
| 278 | + |
| 279 | + BootstrapTable.prototype.refreshGroupByField = function (groupByFields) { |
| 280 | + if (!compareObjects(this.options.groupByField, groupByFields)) { |
| 281 | + this.options.groupByField = groupByFields; |
| 282 | + this.load(this.options.originalData); |
| 283 | + } |
| 284 | + }; |
228 | 285 | }(jQuery);
|
0 commit comments