-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjquery.rocketsorter.js
More file actions
124 lines (104 loc) · 3.89 KB
/
Copy pathjquery.rocketsorter.js
File metadata and controls
124 lines (104 loc) · 3.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*!
* Project: jQuery RocketSorter
* Description: A recursive table sorter
* Author: Davi Reinke
* Contributor: Vinicius Ebersol
* Repository: https://github.com/dreinke/jquery-rocketsorter
* License: MIT License
* http://www.opensource.org/licenses/mit-license.php
*/
(function ($, window, document) {
var defaults = {
parameters: [{
coll: 0,
method: 'alphabetic',
reverse: false
}],
sorters: {
'alphabetic': function (a, b) {
return a > b ? 1 : -1;
},
'numeric': function (a, b) {
a = parseInt(a, 10);
b = parseInt(b, 10);
return a - b;
},
'currency': function (a, b) {
var regex = new RegExp(/[£$€]/g);
a = parseFloat(a.replace(regex, ''));
b = parseFloat(b.replace(regex, ''));
return a - b;
},
'percent': function (a, b) {
var regex = new RegExp(/%/g);
a = parseFloat(a.replace(regex, ''));
b = parseFloat(b.replace(regex, ''));
return a - b;
}
}
};
function Plugin(element, options) {
this.sorters = $.extend(true, defaults.sorters, options.sorters);
this.parameters = $.extend(true, defaults.parameters, options.parameters);
this.table = $(element);
this.tbody = this.table.find('tbody');
this.tableRows = this.tbody.find('tr');
if (options.parameters) {
this.sortBy(this.parameters);
}
}
Plugin.prototype = {
sortBy: function (params) {
var _this = this,
parameters = $.extend(true, this.parameters, params),
rowsLength = this.tableRows.length,
rows = [],
i = 0;
for (i = 0; i < rowsLength; i++) {
rows.push(this.tableRows[i]);
}
rows = this.sortRows(rows, parameters);
for (i = 0; i < rowsLength; i++) {
this.tbody.append(rows[i]);
}
},
sortRows: function (rows, params) {
var i,
_this = this,
parameters = [].concat(params),
param = parameters.shift(),
rowsLength = rows.length;
rows.sort(function (rowA, rowB) {
var a = $(rowA.cells[param.coll]),
b = $(rowB.cells[param.coll]);
if (a.data('value') && b.data('value'))
return _this.sorters[param.method](
a.data('value')? a.data('value') : a.html(),
b.data('value')? b.data('value') : b.html()
);
});
if (param.reverse) {
rows.reverse();
}
if (parameters.length > 0) {
var equalRows = [rows[0]],
result = [];
for (i = 1; i < rowsLength; i++) {
if (equalRows[0].cells[param.coll].innerHTML == rows[i].cells[param.coll].innerHTML) {
equalRows = equalRows.concat(rows[i]);
} else {
result = result.concat(equalRows.length > 1? this.sortRows(equalRows, parameters) : equalRows);
equalRows = [rows[i]];
}
}
result = result.concat(equalRows.length > 1? this.sortRows(equalRows, parameters) : equalRows);
return result;
} else {
return rows;
}
}
};
$.fn.rocketSorter = function (options) {
return new Plugin(this, options? options : {});
};
})(jQuery, window, document);