-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsortable-table.js
48 lines (45 loc) · 1.52 KB
/
sortable-table.js
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
(function( $ ) {
$.fn.makeSortable = function() {
return this.each(function() {
var tbody = $(this).children('tbody')[0];
var cachedTableHTML = tbody.innerHTML;
var priority = [];
var priorityComparator = function(tr1, tr2) {
var tds1 = $(tr1).find('td').toArray();
var tds2 = $(tr2).find('td').toArray();
for (var i = 0; i < priority.length; i++) {
if( tds1[priority[i]].innerText == tds2[ priority[i]].innerText ) {
continue;
} else {
return (tds1[ priority[i] ].innerText > tds2[ priority[i]].innerText) ? 1 : -1;
}
}
return 0;
};
var refresh = function(trs) {
$(tbody).empty();
trs.forEach(function(tr) {
$(tbody).append(tr);
});
};
$(this).find('th').bind("click", function() {
thIndex = $(this).index();
priorityIndex = priority.indexOf(thIndex);
if (priorityIndex == -1) {
priority.push(thIndex);
$(this).css('box-shadow', 'inset 0px 0px 6px 1px #ddd');
} else {
priority.splice(priorityIndex, 1);
$(this).css('box-shadow', 'none');
}
tbody.innerHTML = cachedTableHTML;
if (priority.length != 0) {
var tr = $(tbody).find('tr')
.toArray()
.sort(priorityComparator);
refresh(tr);
}
});
});
};
}( jQuery ));