8000 Added $.fn.bootstrapTable.utils tools. · coderabsolute/bootstrap-table@6410da3 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6410da3

Browse files
committed
Added $.fn.bootstrapTable.utils tools.
1 parent fdb0daf commit 6410da3

File tree

8 files changed

+49
-299
lines changed

8 files changed

+49
-299
lines changed

src/bootstrap-table.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2546,6 +2546,12 @@
25462546
$.fn.bootstrapTable.columnDefaults = BootstrapTable.COLUMN_DEFAULTS;
25472547
$.fn.bootstrapTable.locales = BootstrapTable.LOCALES;
25482548
$.fn.bootstrapTable.methods = allowedMethods;
2549+
$.fn.bootstrapTable.utils = {
2550+
sprintf: sprintf,
2551+
getFieldIndex: getFieldIndex,
2552+
compareObjects: compareObjects,
2553+
calculateObjectValue: calculateObjectValue
2554+
};
25492555

25502556
// BOOTSTRAP TABLE INIT
25512557
// =======================

src/extensions/accent-neutralise/bootstrap-table-accent-neutralise.js

Lines changed: 3 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* @author: Dennis Hernández
2+
* @author: Dennis Hernández
33
* @webSite: http://djhvscf.github.io/Blog
44
* @version: v1.0.0
55
*/
@@ -113,47 +113,6 @@
113113
});
114114
};
115115

116-
var getFieldIndex = function (columns, field) {
117-
var index = -1;
118-
119-
$.each(columns, function (i, column) {
120-
if (column.field === field) {
121-
index = i;
122-
return false;
123-
}
124-
return true;
125-
});
126-
return index;
127-
};
128-
129-
var calculateObjectValue = function (self, name, args, defaultValue) {
130-
var func = name;
131-
132-
if (typeof name === 'string') {
133-
// support obj.func1.func2
134-
var names = name.split('.');
135-
136-
if (names.length > 1) {
137-
func = window;
138-
$.each(names, function (i, f) {
139-
func = func[f];
140-
});
141-
} else {
142-
func = window[name];
143-
}
144-
}
145-
if (typeof func === 'object') {
146-
return func;
147-
}
148-
if (typeof func === 'function') {
149-
return func.apply(self, args);
150-
}
151-
if (!func && typeof name === 'string' && sprintf.apply(this, [name].concat(args))) {
152-
return sprintf.apply(this, [name].concat(args));
153-
}
154-
return defaultValue;
155-
};
156-
157116
$.extend($.fn.bootstrapTable.defaults, {
158117
searchAccentNeutralise: false
159118
});
@@ -190,11 +149,11 @@
190149
for (var key in item) {
191150
key = $.isNumeric(key) ? parseInt(key, 10) : key;
192151
var value = item[key],
193-
column = that.columns[getFieldIndex(that.columns, key)],
152+
column = that.columns[$.fn.bootstrapTable.utils.getFieldIndex(that.columns, key)],
194153
j = $.inArray(key, that.header.fields);
195154

196155
if (column && column.searchFormatter) {
197-
value = calculateObjectValue(column,
156+
value = $.fn.bootstrapTable.utils.calculateObjectValue(column,
198157
that.header.formatters[j], [value, item, i], value);
199158
}
200159

src/extensions/filter-control/bootstrap-table-filter-control.js

Lines changed: 4 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -8,58 +8,7 @@
88

99
'use strict';
1010

11-
var sprintf = function (str) {
12-
var args = arguments,
13-
flag = true,
14-
i = 1;
15-
16-
str = str.replace(/%s/g, function () {
17-
var arg = args[i++];
18-
19-
if (typeof arg === 'undefined') {
20-
flag = false;
21-
return '';
22-
}
23-
return arg;
24-
});
25-
return flag ? str : '';
26-
};
27-
28-
var getFieldIndex = function (columns, field) {
29-
var index = -1;
30-
31-
$.each(columns, function (i, column) {
32-
if (column.field === field) {
33-
index = i;
34-
return false;
35-
}
36-
return true;
37-
});
38-
return index;
39-
};
40-
41-
var calculateObjectValue = function (self, name, args, defaultValue) {
42-
if (typeof name === 'string') {
43-
// support obj.func1.func2
44-
var names = name.split('.');
45-
46-
if (names.length > 1) {
47-
name = window;
48-
$.each(names, function (i, f) {
49-
name = name[f];
50-
});
51-
} else {
52-
name = window[name];
53-
}
54-
}
55-
if (typeof name === 'object') {
56-
return name;
57-
}
58-
if (typeof name === 'function') {
59-
return name.apply(self, args);
60-
}
61-
return defaultValue;
62-
};
11+
var sprintf = $.fn.bootstrapTable.utils.sprintf;
6312

6413
var addOptionToSelectControl = function (selectControl, value, text) {
6514
selectControl = $(selectControl.get(selectControl.length - 1));
@@ -384,9 +333,9 @@
384333

385334
$.each(this.header.fields, function (j, field) {
386335
var value = item[field],
387-
column = that.columns[getFieldIndex(that.columns, field)];
336+
column = that.columns[$.fn.bootstrapTable.utils.getFieldIndex(that.columns, field)];
388337

389-
value = calculateObjectValue(that.header, that.header.formatters[j], [value, item, i], value);
338+
value = $.fn.bootstrapTable.utils.calculateObjectValue(that.header, that.header.formatters[j], [value, item, i], value);
390339

391340
if ((!column.checkbox) || (!column.radio)) {
392341
if (column.filterControl !== undefined && column.filterControl.toLowerCase() === 'select' && column.searchable) {
@@ -419,7 +368,7 @@
419368
for (var key in fp) {
420369
var fval = fp[key].toLowerCase();
421370
var value = item[key];
422-
value = calculateObjectValue(that.header,
371+
value = $.fn.bootstrapTable.utils.calculateObjectValue(that.header,
423372
that.header.formatters[$.inArray(key, that.header.fields)],
424373
[value, item, i], value);
425374

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

Lines changed: 30 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -14,48 +14,6 @@
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-
46-
var getFieldIndex = function (columns, field) {
47-
var index = -1;
48-
49-
$.each(columns, function (i, column) {
50-
if (column.field === field) {
51-
index = i;
52-
return false;
53-
}
54-
return true;
55-
});
56-
return index;
57-
};
58-
5917
var getParentRowId = function (that, id) {
6018
var parentRows = that.$body.find('tr').not('[' + 'data-tt-parent-id]');
6119

@@ -72,15 +30,15 @@
7230
var sumRow = {};
7331
$.each(data, function (i, row) {
7432
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];
33+
for (var prop in row) {
34+
if (!isNaN(parseFloat(row[prop]))) {
35+
if (that.columns[$.fn.bootstrapTable.utils.getFieldIndex(that.columns, prop)].groupBySumGroup) {
36+
if (sumRow[prop] === undefined) {
37+
sumRow[prop] = 0;
8238
}
39+
sumRow[prop] += +row[prop];
8340
}
41+
}
8442
}
8543
}
8644
});
@@ -105,19 +63,19 @@
10563

10664
var setObjectKeys = function () {
10765
// From https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys
108-
Object.keys = function (o) {
109-
if (o !== Object(o)) {
110-
throw new TypeError('Object.keys called on a non-object');
111-
}
112-
var k = [],
113-
p;
114-
for (p in o) {
115-
if (Object.prototype.hasOwnProperty.call(o, p)) {
116-
k.push(p);
117-
}
118-
}
119-
return k;
120-
}
66+
Object.keys = function (o) {
67+
if (o !== Object(o)) {
68+
throw new TypeError('Object.keys called on a non-object');
69+
}
70+
var k = [],
71+
p;
72+
for (p in o) {
73+
if (Object.prototype.hasOwnProperty.call(o, p)) {
74+
k.push(p);
75+
}
76+
}
77+
return k;
78+
}
12179
};
12280

12381
var getDataArrayFromItem = function (that, item) {
@@ -140,16 +98,16 @@
14098
return newRow;
14199
};
142100

143-
var groupBy = function (array , f) {
144-
var groups = {};
145-
$.each(array, function(i, o) {
146-
var group = JSON.stringify(f(o));
147-
groups[group] = groups[group] || [];
148-
groups[group].push(o);
149-
});
150-
return Object.keys(groups).map(function (group) {
101+
var groupBy = function (array, f) {
102+
var groups = {};
103+
$.each(array, function (i, o) {
104+
var group = JSON.stringify(f(o));
105+
groups[group] = groups[group] || [];
106+
groups[group].push(o);
107+
});
108+
return Object.keys(groups).map(function (group) {
151109
return groups[group];
152-
});
110+
});
153111
};
154112

155113
var makeGrouped = function (that, data) {
@@ -277,7 +235,7 @@
277235
};
278236

279237
BootstrapTable.prototype.refreshGroupByField = function (groupByFields) {
280-
if (!compareObjects(this.options.groupByField, groupByFields)) {
238+
if (!$.fn.bootstrapTable.utils.compareObjects(this.options.groupByField, groupByFields)) {
281239
this.options.groupByField = groupByFields;
282240
this.load(this.options.originalData);
283241
}

src/extensions/mobile/bootstrap-table-mobile.js

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,12 @@
88

99
'use strict';
1010

11-
var getFieldIndex = function (columns, field) {
12-
var index = -1;
13-
14-
$.each(columns, function (i, column) {
15-
if (column.field === field) {
16-
index = i;
17-
return false;
18-
}
19-
return true;
20-
});
21-
return index;
22-
};
23-
2411
var showHideColumns = function (that, checked) {
2512
if (that.options.columnsHidden.length > 0 ) {
2613
$.each(that.columns, function (i, column) {
2714
if (that.options.columnsHidden.indexOf(column.field) !== -1) {
2815
if (column.visible !== checked) {
29-
that.toggleColumn(getFieldIndex(that.columns, column.field), checked, true);
16+
that.toggleColumn($.fn.bootstrapTable.utils.getFieldIndex(that.columns, column.field), checked, true);
3017
}
3118
}
3219
});

0 commit comments

Comments
 (0)
0