-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtable-sort.js
executable file
·57 lines (57 loc) · 2.14 KB
/
table-sort.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
49
50
51
52
53
54
55
56
57
function columnSort(n){
var table, rows, switching, i, x,y,shouldSwitch, dir, switchcount=0;
var ref = $("#mainTable thead tr th:nth-child("+(n+1)+")");
var icon = $("#mainTable thead tr th:nth-child("+(n+1)+") #asc");
$(".sort-img").remove();
switch (ref.attr("value")) {
case '2':
dir = "asc";
$("#mainTable thead tr th").attr("value","0");
ref.attr("value",1);
$("<span class='sort-img'>▲</span>").appendTo(ref);
break;
case '1':
dir = "desc";
$("#mainTable thead tr th").attr("value","0");
ref.attr("value",2);
$("<span class='sort-img'>▼</span>").appendTo(ref);
break;
default:
dir = "asc";
$("#mainTable thead tr th").attr("value","0");
ref.attr("value",1);
$("<span class='sort-img'>▲</span>").appendTo(ref);
break;
}
function isNumber(n){
return !isNaN(parseFloat(n)) && isFinite(n);
}
switching = true;
while(switching){
switching = false;
rows = $("#mainTable tbody tr");
// console.log(rows);
for (i=0; i<(rows.length-1) ; i++ ){
shouldSwitch = false;
x = rows[i].getElementsByTagName("td")[n];
y = rows[i+1].getElementsByTagName("td")[n];
// console.log(x+","+y);
if (isNumber(x.innerHTML)){
if ( (dir=="asc" && parseFloat(x.innerHTML) > parseFloat(y.innerHTML)) || (dir=="desc" && parseFloat(x.innerHTML) < parseFloat(y.innerHTML)) ){
shouldSwitch = true;
break;
}
} else {
if ( (dir == "asc" && (x.innerHTML.toLowerCase() > y.innerHTML.toLowerCase()) ) || (dir == "desc" && (x.innerHTML.toLowerCase() < y.innerHTML.toLowerCase()) ) ){
shouldSwitch = true;
break;
}}
}
if (shouldSwitch){
rows[i].parentNode.insertBefore(rows[i+1], rows[i]);
switching = true;
switchcount ++;
}
}
// console.log("count = "+switchcount);
}