-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathjquery.table_sort.js
More file actions
180 lines (158 loc) · 4.22 KB
/
jquery.table_sort.js
File metadata and controls
180 lines (158 loc) · 4.22 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*!
* jquery.table_sort.js
* Table sort plugin for jQuery.
*
* @version v0.1.2
* @author i2bskn
* @license MIT
* @url http://i2bskn.github.io/jquery-table-sort/
*/
;(function(factory) {
if (typeof module === "object" && typeof module.exports === "object") {
module.exports = factory(require("jquery"), window, document);
} else {
factory(jQuery, window, document);
}
}(function($, window, document, undefined) {
var State = (function(){
function State(indexes){
this.index = null;
this.order = 1;
this.indexes = indexes;
}
State.prototype = {
getOrder: function(index){
if (this.index === index) {
return this.order;
} else {
return 1;
}
},
updateOrder: function(index){
if (this.index === index) {
this.order *= -1;
} else {
this.index = index;
this.order = -1;
}
},
getIndex: function(){
return this.index;
},
indexCheck: function(index){
if (this.indexes.indexOf(index) !== -1){
return true;
} else {
return false;
}
},
};
return State;
})();
$.fn.tableSort = function(params){
var defaults = {
indexes: $(this).find("thead").find("th").map(function(){
return $(this).index();
}).get(),
compare: function(){},
after: function(){},
};
var settings = $.extend(defaults, params);
var state = new State(settings.indexes);
var $target = $(this);
var compare = function(a, b, type, index) {
var _a = $(a).find("td").eq(index).text();
var _b = $(b).find("td").eq(index).text();
switch (type){
case "integer":
_a *= 1;
_b *= 1;
return _a - _b;
case "date":
var _date_a = new Date(_a).getTime();
var _date_b = new Date(_b).getTime();
if (_date_a < _date_b) {
return -1;
} else if (_date_a > _date_b) {
return 1;
}
return 0;
case "custom":
return settings.compare(_a, _b);
default:
if (_a < _b) {
return -1;
} else if (_a > _b) {
return 1;
}
return 0;
}
};
var removeClass = function(target){
$(target).removeClass("sort_column_default sort_column_asc sort_column_desc");
};
var addClass = function(target){
var $inner = $(target);
var index = $inner.index();
if (state.getIndex() === index){
if (state.getOrder(index) === -1){
$inner.addClass("sort_column_asc");
} else {
$inner.addClass("sort_column_desc");
}
} else {
$inner.addClass("sort_column_default");
}
};
var refreshClass = function(target){
$(target).find("thead").find("th").each(function(){
if (state.indexCheck($(this).index())){
removeClass(this);
addClass(this);
}
});
};
var sort = function(){
var type = $(this).data("type");
var index = $(this).index();
var $rows = $target.find("tbody>tr");
$rows.sort(function(a, b){
return compare(a, b, type, index) * state.getOrder(index);
});
$target.find("tbody").empty().append($rows);
state.updateOrder(index);
refreshClass($target);
settings.after(this);
};
$target.find("thead").find("th").each(function(){
if (state.indexCheck($(this).index())){
$(this).on("click", sort);
}
});
refreshClass($target);
return this;
};
$.fn.tableMove = function(params){
var defaults = {
after: function(){},
};
var settings = $.extend(defaults, params);
var moveUp = function(){
var $row = $(this).closest("tr");
if ($row.prev("tr").length){
$row.insertBefore($row.prev("tr"));
settings.after($row);
}
};
var moveDown = function(){
var $row = $(this).closest("tr");
if ($row.next("tr").length){
$row.insertAfter($row.next("tr"));
settings.after($row);
}
};
$(this).find("tbody>tr").find(".up").on("click", moveUp);
$(this).find("tbody>tr").find(".down").on("click", moveDown);
return this;
};
}));