0.2.3
by James Westby
Import upstream version 3.1.0 |
1 |
// Used with under license grant from http://kryogenix.org/code/browser/sorttable/
|
2 |
// Credit for this code goes to Stuart Langridge - http://kryogenix.org/contact
|
|
3 |
||
4 |
addEvent(window, "load", sortables_init); |
|
5 |
||
6 |
var SORT_COLUMN_INDEX; |
|
7 |
||
8 |
function sortables_init() { |
|
9 |
// Find all tables with class sortable and make them sortable
|
|
10 |
if (!document.getElementsByTagName) return; |
|
11 |
tbls = document.getElementsByTagName("table"); |
|
12 |
for (ti=0;ti<tbls.length;ti++) { |
|
13 |
thisTbl = tbls[ti]; |
|
14 |
if (((' '+thisTbl.className+' ').indexOf("sortable") != -1) && (thisTbl.id)) { |
|
15 |
//initTable(thisTbl.id);
|
|
16 |
ts_makeSortable(thisTbl); |
|
17 |
}
|
|
18 |
}
|
|
19 |
}
|
|
20 |
||
21 |
function ts_makeSortable(table) { |
|
22 |
if (table.rows && table.rows.length > 0) { |
|
23 |
var firstRow = table.rows[0]; |
|
24 |
}
|
|
25 |
if (!firstRow) return; |
|
26 |
||
27 |
// We have a first row: assume it's the header, and make its contents clickable links
|
|
28 |
for (var i=0;i<firstRow.cells.length;i++) { |
|
29 |
var cell = firstRow.cells[i]; |
|
30 |
var txt = ts_getInnerText(cell); |
|
31 |
cell.innerHTML = '<a href="#" class="sortheader" '+ |
|
32 |
'onclick="ts_resortTable(this, '+i+');return false;">' + |
|
33 |
txt+'<span class="sortarrow"> </span></a>'; |
|
34 |
}
|
|
35 |
}
|
|
36 |
||
37 |
function ts_getInnerText(el) { |
|
38 |
if (typeof el == "string") return el; |
|
39 |
if (typeof el == "undefined") { return el }; |
|
40 |
if (el.innerText) return el.innerText; //Not needed but it is faster |
|
41 |
var str = ""; |
|
42 |
||
43 |
var cs = el.childNodes; |
|
44 |
var l = cs.length; |
|
45 |
for (var i = 0; i < l; i++) { |
|
46 |
switch (cs[i].nodeType) { |
|
47 |
case 1: //ELEMENT_NODE |
|
48 |
str += ts_getInnerText(cs[i]); |
|
49 |
break; |
|
50 |
case 3: //TEXT_NODE |
|
51 |
str += cs[i].nodeValue; |
|
52 |
break; |
|
53 |
}
|
|
54 |
}
|
|
55 |
return str; |
|
56 |
}
|
|
57 |
||
58 |
function ts_resortTable(lnk,clid) { |
|
59 |
// get the span
|
|
60 |
var span; |
|
61 |
for (var ci=0;ci<lnk.childNodes.length;ci++) { |
|
62 |
if (lnk.childNodes[ci].tagName && lnk.childNodes[ci].tagName.toLowerCase() == 'span') span = lnk.childNodes[ci]; |
|
63 |
}
|
|
64 |
var spantext = ts_getInnerText(span); |
|
65 |
var td = lnk.parentNode; |
|
66 |
var column = clid || td.cellIndex; |
|
67 |
var table = getParent(td,'TABLE'); |
|
68 |
||
69 |
// Work out a type for the column
|
|
70 |
if (table.rows.length <= 1) return; |
|
71 |
var itm = ts_getInnerText(table.rows[1].cells[column]); |
|
72 |
sortfn = ts_sort_caseinsensitive; |
|
73 |
if (itm.match(/^\d\d[\/-]\d\d[\/-]\d\d\d\d$/)) sortfn = ts_sort_date; |
|
74 |
if (itm.match(/^\d\d[\/-]\d\d[\/-]\d\d$/)) sortfn = ts_sort_date; |
|
75 |
if (itm.match(/^[£$]/)) sortfn = ts_sort_currency; |
|
76 |
if (itm.match(/^[\d\.]+$/)) sortfn = ts_sort_numeric; |
|
77 |
SORT_COLUMN_INDEX = column; |
|
78 |
var firstRow = new Array(); |
|
79 |
var newRows = new Array(); |
|
80 |
for (i=0;i<table.rows[0].length;i++) { firstRow[i] = table.rows[0][i]; } |
|
81 |
for (j=1;j<table.rows.length;j++) { newRows[j-1] = table.rows[j]; } |
|
82 |
||
83 |
newRows.sort(sortfn); |
|
84 |
||
85 |
if (span.getAttribute("sortdir") == 'down') { |
|
86 |
ARROW = ' ↑'; |
|
87 |
newRows.reverse(); |
|
88 |
span.setAttribute('sortdir','up'); |
|
89 |
} else { |
|
90 |
ARROW = ' ↓'; |
|
91 |
span.setAttribute('sortdir','down'); |
|
92 |
}
|
|
93 |
||
94 |
// We appendChild rows that already exist to the tbody, so it moves them rather than creating new ones
|
|
95 |
// don't do sortbottom rows
|
|
96 |
for (i=0;i<newRows.length;i++) { if (!newRows[i].className || (newRows[i].className && (newRows[i].className.indexOf('sortbottom') == -1))) table.tBodies[0].appendChild(newRows[i]);} |
|
97 |
// do sortbottom rows only
|
|
98 |
for (i=0;i<newRows.length;i++) { if (newRows[i].className && (newRows[i].className.indexOf('sortbottom') != -1)) table.tBodies[0].appendChild(newRows[i]);} |
|
99 |
||
100 |
// Delete any other arrows there may be showing
|
|
101 |
var allspans = document.getElementsByTagName("span"); |
|
102 |
for (var ci=0;ci<allspans.length;ci++) { |
|
103 |
if (allspans[ci].className == 'sortarrow') { |
|
104 |
if (getParent(allspans[ci],"table") == getParent(lnk,"table")) { // in the same table as us? |
|
105 |
allspans[ci].innerHTML = ' '; |
|
106 |
}
|
|
107 |
}
|
|
108 |
}
|
|
109 |
||
110 |
span.innerHTML = ARROW; |
|
111 |
}
|
|
112 |
||
113 |
function getParent(el, pTagName) { |
|
114 |
if (el == null) return null; |
|
115 |
else if (el.nodeType == 1 && el.tagName.toLowerCase() == pTagName.toLowerCase()) // Gecko bug, supposed to be uppercase |
|
116 |
return el; |
|
117 |
else
|
|
118 |
return getParent(el.parentNode, pTagName); |
|
119 |
}
|
|
120 |
function ts_sort_date(a,b) { |
|
121 |
// y2k notes: two digit years less than 50 are treated as 20XX, greater than 50 are treated as 19XX
|
|
122 |
aa = ts_getInnerText(a.cells[SORT_COLUMN_INDEX]); |
|
123 |
bb = ts_getInnerText(b.cells[SORT_COLUMN_INDEX]); |
|
124 |
if (aa.length == 10) { |
|
125 |
dt1 = aa.substr(6,4)+aa.substr(3,2)+aa.substr(0,2); |
|
126 |
} else { |
|
127 |
yr = aa.substr(6,2); |
|
128 |
if (parseInt(yr) < 50) { yr = '20'+yr; } else { yr = '19'+yr; } |
|
129 |
dt1 = yr+aa.substr(3,2)+aa.substr(0,2); |
|
130 |
}
|
|
131 |
if (bb.length == 10) { |
|
132 |
dt2 = bb.substr(6,4)+bb.substr(3,2)+bb.substr(0,2); |
|
133 |
} else { |
|
134 |
yr = bb.substr(6,2); |
|
135 |
if (parseInt(yr) < 50) { yr = '20'+yr; } else { yr = '19'+yr; } |
|
136 |
dt2 = yr+bb.substr(3,2)+bb.substr(0,2); |
|
137 |
}
|
|
138 |
if (dt1==dt2) return 0; |
|
139 |
if (dt1<dt2) return -1; |
|
140 |
return 1; |
|
141 |
}
|
|
142 |
||
143 |
function ts_sort_currency(a,b) { |
|
144 |
aa = ts_getInnerText(a.cells[SORT_COLUMN_INDEX]).replace(/[^0-9.]/g,''); |
|
145 |
bb = ts_getInnerText(b.cells[SORT_COLUMN_INDEX]).replace(/[^0-9.]/g,''); |
|
146 |
return parseFloat(aa) - parseFloat(bb); |
|
147 |
}
|
|
148 |
||
149 |
function ts_sort_numeric(a,b) { |
|
150 |
aa = parseFloat(ts_getInnerText(a.cells[SORT_COLUMN_INDEX])); |
|
151 |
if (isNaN(aa)) aa = 0; |
|
152 |
bb = parseFloat(ts_getInnerText(b.cells[SORT_COLUMN_INDEX])); |
|
153 |
if (isNaN(bb)) bb = 0; |
|
154 |
return aa-bb; |
|
155 |
}
|
|
156 |
||
157 |
function ts_sort_caseinsensitive(a,b) { |
|
158 |
aa = ts_getInnerText(a.cells[SORT_COLUMN_INDEX]).toLowerCase(); |
|
159 |
bb = ts_getInnerText(b.cells[SORT_COLUMN_INDEX]).toLowerCase(); |
|
160 |
if (aa==bb) return 0; |
|
161 |
if (aa<bb) return -1; |
|
162 |
return 1; |
|
163 |
}
|
|
164 |
||
165 |
function ts_sort_default(a,b) { |
|
166 |
aa = ts_getInnerText(a.cells[SORT_COLUMN_INDEX]); |
|
167 |
bb = ts_getInnerText(b.cells[SORT_COLUMN_INDEX]); |
|
168 |
if (aa==bb) return 0; |
|
169 |
if (aa<bb) return -1; |
|
170 |
return 1; |
|
171 |
}
|
|
172 |
||
173 |
||
174 |
function addEvent(elm, evType, fn, useCapture) |
|
175 |
// addEvent and removeEvent
|
|
176 |
// cross-browser event handling for IE5+, NS6 and Mozilla
|
|
177 |
// By Scott Andrew
|
|
178 |
{
|
|
179 |
if (elm.addEventListener){ |
|
180 |
elm.addEventListener(evType, fn, useCapture); |
|
181 |
return true; |
|
182 |
} else if (elm.attachEvent){ |
|
183 |
var r = elm.attachEvent("on"+evType, fn); |
|
184 |
return r; |
|
185 |
} else { |
|
186 |
alert("Handler could not be removed"); |
|
187 |
}
|
|
188 |
}
|