Skip to content

Commit 342a72e

Browse files
committed
Merge pull request wenzhixin#1381 from djhvscf/master
Added support for multiple group fields and added the refreshGroupField method
2 parents 5515ad6 + 336baca commit 342a72e

File tree

2 files changed

+82
-25
lines changed

2 files changed

+82
-25
lines changed

src/extensions/group-by/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ You must include the bootstrap-table-group-by.css file in order to get the appro
2020

2121
### groupByField
2222

23-
* type: String
24-
* description: Set the field that you want to group the data.
25-
* default: ``
23+
* type: Array
24+
* description: Set the array fields that you want to group the data.
25+
* default: `[]`
2626

2727
### groupBySumGroup
2828

src/extensions/group-by/bootstrap-table-group-by.js

Lines changed: 79 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @author: Dennis Hernández
33
* @webSite: http://djhvscf.github.io/Blog
4-
* @version: v1.0.0
4+
* @version: v1.1.0
55
*/
66

77
!function ($) {
@@ -14,6 +14,35 @@
1414
obj = {},
1515
parentId = undefined;
1616

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+
1746
var getFieldIndex = function (columns, field) {
1847
var index = -1;
1948

@@ -42,16 +71,16 @@
4271
var sumData = function (that, data) {
4372
var sumRow = {};
4473
$.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];
5182
}
52-
sumRow[prop] += +row[prop];
5383
}
54-
}
5584
}
5685
}
5786
});
@@ -91,6 +120,26 @@
91120
}
92121
};
93122

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+
94143
var groupBy = function (array , f) {
95144
var groups = {};
96145
$.each(array, function(i, o) {
@@ -104,25 +153,21 @@
104153
};
105154

106155
var makeGrouped = function (that, data) {
107-
var newRow = {},
108-
newData = [],
156+
var newData = [],
109157
sumRow = {};
110158

111159
var result = groupBy(data, function (item) {
112-
return [item[that.options.groupByField]];
160+
return getDataArrayFromItem(that, item);
113161
});
114162

115163
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));
119165
if (that.options.groupBySumGroup) {
120-
sumRow = sumData(that, result[i])
166+
sumRow = sumData(that, result[i]);
121167
if (!$.isEmptyObject(sumRow)) {
122168
result[i].push(sumRow);
123169
}
124170
}
125-
newRow = {};
126171
}
127172

128173
newData = newData.concat.apply(newData, result);
@@ -138,15 +183,15 @@
138183

139184
$.extend($.fn.bootstrapTable.defaults, {
140185
groupBy: false,
141-
groupByField: '',
186+
groupByField: [],
142187
groupBySumGroup: false,
143188
groupByInitExpanded: undefined, //node, 'all'
144189
//internal variables
145190
loaded: false,
146191
originalData: undefined
147192
});
148193

149-
$.fn.bootstrapTable.methods.push('collapseAll', 'expandAll');
194+
$.fn.bootstrapTable.methods.push('collapseAll', 'expandAll', 'refreshGroupByField');
150195

151196
$.extend($.fn.bootstrapTable.COLUMN_DEFAULTS, {
152197
groupBySumGroup: false
@@ -159,7 +204,7 @@
159204
BootstrapTable.prototype.init = function () {
160205
//Temporal validation
161206
if (!this.options.sortName) {
162-
if ((this.options.groupBy) && (this.options.groupByField !== '')) {
207+
if ((this.options.groupBy) && (this.options.groupByField.length > 0)) {
163208
var that = this;
164209

165210
// Compatibility: IE < 9 and old browsers
@@ -204,7 +249,12 @@
204249
BootstrapTable.prototype.initData = function (data, type) {
205250
//Temporal validation
206251
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+
208258
data = makeGrouped(this, data ? data : this.options.data);
209259
}
210260
}
@@ -224,5 +274,12 @@
224274
if (id !== undefined) {
225275
this.$el.treetable('expandNode', id);
226276
}
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+
};
228285
}(jQuery);

0 commit comments

Comments
 (0)