2 Table sorting script by Joost de Valk, check it out at http://www.joostdevalk.nl/code/sortable-table/.
3 Based on a script from http://www.kryogenix.org/code/browser/sorttable/.
5 Version 1.5.7 + FusionForge
7 * Copyright (c) 1997-2007 Stuart Langridge, Joost de Valk
8 * Copyright (c) 2012 Thorsten Glaser <t.glaser@tarent.de>
10 * Permission is hereby granted, free of charge, to any person obtaining
11 * a copy of this software and associated documentation files (the
12 * "Software"), to deal in the Software without restriction, including
13 * without limitation the rights to use, copy, modify, merge, publish,
14 * distribute, sublicense, and/or sell copies of the Software, and to
15 * permit persons to whom the Software is furnished to do so, subject to
16 * the following conditions:
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
22 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 /* You can change these values */
28 var image_path = "/images/";
29 var image_up = "sort_up.gif";
30 var image_down = "sort_down.gif";
31 var image_none = "sort_none.gif";
32 var europeandate = true;
33 var alternate_row_colors = true;
35 /* Don't change anything below this unless you know what you're doing */
36 addEvent(window, "load", sortables_init);
38 Array.prototype.sortStable = function(ocmpfn, orev) {
39 /* A stable sort function to allow multi-level sorting of data */
40 /* see: http://en.wikipedia.org/wiki/Cocktail_sort */
41 /* thanks to Joseph Nahmias */
42 /* Reverse sorting support by Thorsten "mirabilos" Glaser */
44 var t = this.length - 1;
49 cmpfn = function(a, b) {
56 for (var i = b; i < t; ++i) {
57 if (cmpfn(this[i], this[i + 1]) > 0) {
59 this[i] = this[i + 1];
70 for (var i = t; i > b; --i) {
71 if (cmpfn(this[i], this[i - 1]) < 0) {
73 this[i] = this[i - 1];
82 var SORT_COLUMN_INDEX;
85 function sortables_init() {
86 // Find all tables with class sortable and make them sortable
87 if (!document.getElementsByTagName) return;
88 tbls = document.getElementsByTagName("table");
89 for (ti=0;ti<tbls.length;ti++) {
91 if ((' '+thisTbl.className+' ').indexOf("sortable") != -1) {
92 ts_makeSortable(thisTbl);
97 function ts_makeSortable(t) {
98 if (t.rows && t.rows.length > 0) {
99 if (t.tHead && t.tHead.rows.length > 0) {
100 var firstRow = t.tHead.rows[t.tHead.rows.length-1];
103 var firstRow = t.rows[0];
106 if (!firstRow) return;
108 // We have a first row: assume it's the header, and make its contents clickable links
109 for (var i=0;i<firstRow.cells.length;i++) {
110 var cell = firstRow.cells[i];
111 var txt = ts_getInnerText(cell);
112 if (cell.className != "unsortable" && cell.className.indexOf("unsortable") == -1 && txt != "") {
113 cell.innerHTML = '<a href="#" class="sortheader" onclick="ts_resortTable(this, '+i+');return false;">'+txt+'<span class="sortarrow"> <img border="0" src="'+ image_path + image_none + '" alt="↓"/></span></a>';
116 if (alternate_row_colors) {
121 function ts_getInnerText(el) {
122 if (typeof el == "string") return el;
123 if (typeof el == "undefined") { return el };
124 if (el.hasAttribute("content")) return el.getAttribute("content");
125 if (el.innerText) return el.innerText; //Not needed but it is faster
128 var cs = el.childNodes;
130 for (var i = 0; i < l; i++) {
131 switch (cs[i].nodeType) {
132 case 1: //ELEMENT_NODE
133 str += ts_getInnerText(cs[i]);
136 str += cs[i].nodeValue;
143 function ts_resortTable(lnk, clid) {
145 for (var ci=0;ci<lnk.childNodes.length;ci++) {
146 if (lnk.childNodes[ci].tagName && lnk.childNodes[ci].tagName.toLowerCase() == 'span') span = lnk.childNodes[ci];
148 var spantext = ts_getInnerText(span);
149 var td = lnk.parentNode;
150 var column = clid || td.cellIndex;
151 var t = getParent(td,'TABLE');
152 // Work out a type for the column
153 if (t.rows.length <= 1) return;
156 while (itm == "" && i < t.tBodies[0].rows.length) {
157 var itm = ts_getInnerText(t.tBodies[0].rows[i].cells[column]);
159 if (itm.substr(0,4) == "<!--" || itm.length == 0) {
164 if (itm == "") return;
165 sortfn = ts_sort_caseinsensitive;
166 if (itm.match(/^\d\d[\/\.-][a-zA-z][a-zA-Z][a-zA-Z][\/\.-]\d\d\d\d$/)) sortfn = ts_sort_date;
167 if (itm.match(/^\d\d[\/\.-]\d\d[\/\.-]\d\d\d{2}?$/)) sortfn = ts_sort_date;
168 if (itm.match(/^-?[\u00A3$\u20AC\u00A2\u00A5]\d/)) sortfn = ts_sort_numeric;
169 // ape: added to provide numeric sort on size for the docs tools.
170 if (itm.match(/^\d+ *(B|KB|MB)$/)) sortfn = ts_sort_numeric;
171 if (itm.match(/^-?(\d+[,\.]?)+(E[-+][\d]+)?%?$/)) sortfn = ts_sort_numeric;
172 SORT_COLUMN_INDEX = column;
173 var firstRow = new Array();
174 var newRows = new Array();
175 for (k=0;k<t.tBodies.length;k++) {
176 for (i=0;i<t.tBodies[k].rows[0].length;i++) {
177 firstRow[i] = t.tBodies[k].rows[0][i];
180 for (k=0;k<t.tBodies.length;k++) {
182 // Skip the first row
183 for (j=1;j<t.tBodies[k].rows.length;j++) {
184 newRows[j-1] = t.tBodies[k].rows[j];
187 // Do NOT skip the first row
188 for (j=0;j<t.tBodies[k].rows.length;j++) {
189 newRows[j] = t.tBodies[k].rows[j];
193 var sortReverse = (span.getAttribute("sortdir") == 'down');
194 newRows.sortStable(sortfn, sortReverse);
196 ARROW = ' <img border="0" src="'+ image_path + image_down + '" alt="↓"/>';
197 span.setAttribute('sortdir','up');
199 ARROW = ' <img border="0" src="'+ image_path + image_up + '" alt="↑"/>';
200 span.setAttribute('sortdir','down');
202 // We appendChild rows that already exist to the tbody, so it moves them rather than creating new ones
203 // don't do sortbottom rows
204 for (i=0; i<newRows.length; i++) {
205 if (!newRows[i].className || (newRows[i].className && (newRows[i].className.indexOf('sortbottom') == -1))) {
206 t.tBodies[0].appendChild(newRows[i]);
209 // do sortbottom rows only
210 for (i=0; i<newRows.length; i++) {
211 if (newRows[i].className && (newRows[i].className.indexOf('sortbottom') != -1))
212 t.tBodies[0].appendChild(newRows[i]);
214 // Delete any other arrows there may be showing
215 var allspans = document.getElementsByTagName("span");
216 for (var ci=0;ci<allspans.length;ci++) {
217 if (allspans[ci].className == 'sortarrow') {
218 if (getParent(allspans[ci],"table") == getParent(lnk,"table")) { // in the same table as us?
219 allspans[ci].innerHTML = ' <img border="0" src="'+ image_path + image_none + '" alt="↓"/>';
223 span.innerHTML = ARROW;
227 function getParent(el, pTagName) {
230 } else if (el.nodeType == 1 && el.tagName.toLowerCase() == pTagName.toLowerCase()) {
233 return getParent(el.parentNode, pTagName);
237 function sort_date(date) {
238 // y2k notes: two digit years less than 50 are treated as 20XX, greater than 50 are treated as 19XX
240 if (date.length == 11) {
241 mtstr = date.substr(3,3);
242 mtstr = mtstr.toLowerCase();
244 case "jan": var mt = "01"; break;
245 case "feb": var mt = "02"; break;
246 case "mar": var mt = "03"; break;
247 case "apr": var mt = "04"; break;
248 case "may": var mt = "05"; break;
249 case "jun": var mt = "06"; break;
250 case "jul": var mt = "07"; break;
251 case "aug": var mt = "08"; break;
252 case "sep": var mt = "09"; break;
253 case "oct": var mt = "10"; break;
254 case "nov": var mt = "11"; break;
255 case "dec": var mt = "12"; break;
256 // default: var mt = "00";
258 dt = date.substr(7,4)+mt+date.substr(0,2);
260 } else if (date.length == 10) {
261 if (europeandate == false) {
262 dt = date.substr(6,4)+date.substr(0,2)+date.substr(3,2);
265 dt = date.substr(6,4)+date.substr(3,2)+date.substr(0,2);
268 } else if (date.length == 8) {
269 yr = date.substr(6,2);
270 if (parseInt(yr) < 50) {
275 if (europeandate == true) {
276 dt = yr+date.substr(3,2)+date.substr(0,2);
279 dt = yr+date.substr(0,2)+date.substr(3,2);
286 function ts_sort_date(a,b) {
287 dt1 = sort_date(ts_getInnerText(a.cells[SORT_COLUMN_INDEX]));
288 dt2 = sort_date(ts_getInnerText(b.cells[SORT_COLUMN_INDEX]));
298 function ts_sort_numeric(a,b) {
299 var aa = ts_getInnerText(a.cells[SORT_COLUMN_INDEX]);
301 var bb = ts_getInnerText(b.cells[SORT_COLUMN_INDEX]);
303 return compare_numeric(aa,bb);
305 function compare_numeric(a,b) {
306 var aa = parseFloat(a);
307 aa = (isNaN(aa) ? 0 : aa);
308 var bb = parseFloat(b);
309 bb = (isNaN(bb) ? 0 : bb);
312 function ts_sort_caseinsensitive(a,b) {
313 var aa = ts_getInnerText(a.cells[SORT_COLUMN_INDEX]).toLowerCase();
314 var bb = ts_getInnerText(b.cells[SORT_COLUMN_INDEX]).toLowerCase();
323 function ts_sort_default(a,b) {
324 var aa = ts_getInnerText(a.cells[SORT_COLUMN_INDEX]);
325 var bb = ts_getInnerText(b.cells[SORT_COLUMN_INDEX]);
334 function addEvent(elm, evType, fn, useCapture)
335 // addEvent and removeEvent
336 // cross-browser event handling for IE5+, NS6 and Mozilla
339 if (elm.addEventListener){
340 elm.addEventListener(evType, fn, useCapture);
342 } else if (elm.attachEvent){
343 var r = elm.attachEvent("on"+evType, fn);
346 alert("Handler could not be removed");
350 function clean_num(str) {
351 str = str.replace(new RegExp(/[^-?0-9.]/g),"");
355 return s.replace(/^\s+|\s+$/g, "");
357 function alternate(table) {
358 // Take object table and get all it's tbodies.
359 var tableBodies = table.getElementsByTagName("tbody");
360 // Loop through these tbodies
361 for (var i = 0; i < tableBodies.length; i++) {
362 // Take the tbody, and get all it's rows
363 var tableRows = tableBodies[i].getElementsByTagName("tr");
364 // Loop through these rows
365 // Start at 1 because we want to leave the heading row untouched
366 for (var j = 0; j < tableRows.length; j++) {
367 // Check if j is even, and apply classes for both possible results
368 if ( (j % 2) == 0 ) {
369 if ( !(tableRows[j].className.indexOf('odd') == -1) ) {
370 tableRows[j].className = tableRows[j].className.replace('odd', 'even');
372 if ( tableRows[j].className.indexOf('even') == -1 ) {
373 tableRows[j].className += " even";
377 if ( !(tableRows[j].className.indexOf('even') == -1) ) {
378 tableRows[j].className = tableRows[j].className.replace('even', 'odd');
380 if ( tableRows[j].className.indexOf('odd') == -1 ) {
381 tableRows[j].className += " odd";