1 | /* |
---|
2 | SortTable |
---|
3 | version 2 |
---|
4 | 7th April 2007 |
---|
5 | Stuart Langridge, http://www.kryogenix.org/code/browser/sorttable/ |
---|
6 | |
---|
7 | Instructions: |
---|
8 | Download this file |
---|
9 | Add <script src="sorttable.js"></script> to your HTML |
---|
10 | Add class="sortable" to any table you'd like to make sortable |
---|
11 | Click on the headers to sort |
---|
12 | |
---|
13 | Thanks to many, many people for contributions and suggestions. |
---|
14 | Licenced as X11: http://www.kryogenix.org/code/browser/licence.html |
---|
15 | This basically means: do what you want with it. |
---|
16 | */ |
---|
17 | |
---|
18 | |
---|
19 | var stIsIE = /*@cc_on!@*/false; |
---|
20 | |
---|
21 | sorttable = { |
---|
22 | init: function() { |
---|
23 | // quit if this function has already been called |
---|
24 | if (arguments.callee.done) return; |
---|
25 | // flag this function so we don't do the same thing twice |
---|
26 | arguments.callee.done = true; |
---|
27 | // kill the timer |
---|
28 | if (_timer) clearInterval(_timer); |
---|
29 | |
---|
30 | if (!document.createElement || !document.getElementsByTagName) return; |
---|
31 | |
---|
32 | sorttable.DATE_RE = /^(\d\d?)[\/\.-](\d\d?)[\/\.-]((\d\d)?\d\d)$/; |
---|
33 | |
---|
34 | forEach(document.getElementsByTagName('table'), function(table) { |
---|
35 | if (table.className.search(/\bsortable\b/) != -1) { |
---|
36 | sorttable.makeSortable(table); |
---|
37 | } |
---|
38 | }); |
---|
39 | |
---|
40 | }, |
---|
41 | |
---|
42 | makeSortable: function(table) { |
---|
43 | if (table.getElementsByTagName('thead').length == 0) { |
---|
44 | // table doesn't have a tHead. Since it should have, create one and |
---|
45 | // put the first table row in it. |
---|
46 | the = document.createElement('thead'); |
---|
47 | the.appendChild(table.rows[0]); |
---|
48 | table.insertBefore(the,table.firstChild); |
---|
49 | } |
---|
50 | // Safari doesn't support table.tHead, sigh |
---|
51 | if (table.tHead == null) table.tHead = table.getElementsByTagName('thead')[0]; |
---|
52 | |
---|
53 | if (table.tHead.rows.length != 1) return; // can't cope with two header rows |
---|
54 | |
---|
55 | // Sorttable v1 put rows with a class of "sortbottom" at the bottom (as |
---|
56 | // "total" rows, for example). This is B&R, since what you're supposed |
---|
57 | // to do is put them in a tfoot. So, if there are sortbottom rows, |
---|
58 | // for backwards compatibility, move them to tfoot (creating it if needed). |
---|
59 | sortbottomrows = []; |
---|
60 | for (var i=0; i<table.rows.length; i++) { |
---|
61 | if (table.rows[i].className.search(/\bsortbottom\b/) != -1) { |
---|
62 | sortbottomrows[sortbottomrows.length] = table.rows[i]; |
---|
63 | } |
---|
64 | } |
---|
65 | if (sortbottomrows) { |
---|
66 | if (table.tFoot == null) { |
---|
67 | // table doesn't have a tfoot. Create one. |
---|
68 | tfo = document.createElement('tfoot'); |
---|
69 | table.appendChild(tfo); |
---|
70 | } |
---|
71 | for (var i=0; i<sortbottomrows.length; i++) { |
---|
72 | tfo.appendChild(sortbottomrows[i]); |
---|
73 | } |
---|
74 | delete sortbottomrows; |
---|
75 | } |
---|
76 | |
---|
77 | // work through each column and calculate its type |
---|
78 | headrow = table.tHead.rows[0].cells; |
---|
79 | for (var i=0; i<headrow.length; i++) { |
---|
80 | // manually override the type with a sorttable_type attribute |
---|
81 | if (!headrow[i].className.match(/\bsorttable_nosort\b/)) { // skip this col |
---|
82 | mtch = headrow[i].className.match(/\bsorttable_([a-z0-9]+)\b/); |
---|
83 | if (mtch) { override = mtch[1]; } |
---|
84 | if (mtch && typeof sorttable["sort_"+override] == 'function') { |
---|
85 | headrow[i].sorttable_sortfunction = sorttable["sort_"+override]; |
---|
86 | } else { |
---|
87 | headrow[i].sorttable_sortfunction = sorttable.guessType(table,i); |
---|
88 | } |
---|
89 | // make it clickable to sort |
---|
90 | headrow[i].sorttable_columnindex = i; |
---|
91 | headrow[i].sorttable_tbody = table.tBodies[0]; |
---|
92 | dean_addEvent(headrow[i],"click", function(e) { |
---|
93 | |
---|
94 | if (this.className.search(/\bsorttable_sorted\b/) != -1) { |
---|
95 | // if we're already sorted by this column, just |
---|
96 | // reverse the table, which is quicker |
---|
97 | sorttable.reverse(this.sorttable_tbody); |
---|
98 | this.className = this.className.replace('sorttable_sorted', |
---|
99 | 'sorttable_sorted_reverse'); |
---|
100 | this.removeChild(document.getElementById('sorttable_sortfwdind')); |
---|
101 | sortrevind = document.createElement('span'); |
---|
102 | sortrevind.id = "sorttable_sortrevind"; |
---|
103 | sortrevind.innerHTML = stIsIE ? ' <font face="webdings">5</font>' : ' ▴'; |
---|
104 | this.appendChild(sortrevind); |
---|
105 | return; |
---|
106 | } |
---|
107 | if (this.className.search(/\bsorttable_sorted_reverse\b/) != -1) { |
---|
108 | // if we're already sorted by this column in reverse, just |
---|
109 | // re-reverse the table, which is quicker |
---|
110 | sorttable.reverse(this.sorttable_tbody); |
---|
111 | this.className = this.className.replace('sorttable_sorted_reverse', |
---|
112 | 'sorttable_sorted'); |
---|
113 | this.removeChild(document.getElementById('sorttable_sortrevind')); |
---|
114 | sortfwdind = document.createElement('span'); |
---|
115 | sortfwdind.id = "sorttable_sortfwdind"; |
---|
116 | sortfwdind.innerHTML = stIsIE ? ' <font face="webdings">6</font>' : ' ▾'; |
---|
117 | this.appendChild(sortfwdind); |
---|
118 | return; |
---|
119 | } |
---|
120 | |
---|
121 | // remove sorttable_sorted classes |
---|
122 | theadrow = this.parentNode; |
---|
123 | forEach(theadrow.childNodes, function(cell) { |
---|
124 | if (cell.nodeType == 1) { // an element |
---|
125 | cell.className = cell.className.replace('sorttable_sorted_reverse',''); |
---|
126 | cell.className = cell.className.replace('sorttable_sorted',''); |
---|
127 | } |
---|
128 | }); |
---|
129 | sortfwdind = document.getElementById('sorttable_sortfwdind'); |
---|
130 | if (sortfwdind) { sortfwdind.parentNode.removeChild(sortfwdind); } |
---|
131 | sortrevind = document.getElementById('sorttable_sortrevind'); |
---|
132 | if (sortrevind) { sortrevind.parentNode.removeChild(sortrevind); } |
---|
133 | |
---|
134 | this.className += ' sorttable_sorted'; |
---|
135 | sortfwdind = document.createElement('span'); |
---|
136 | sortfwdind.id = "sorttable_sortfwdind"; |
---|
137 | sortfwdind.innerHTML = stIsIE ? ' <font face="webdings">6</font>' : ' ▾'; |
---|
138 | this.appendChild(sortfwdind); |
---|
139 | |
---|
140 | // build an array to sort. This is a Schwartzian transform thing, |
---|
141 | // i.e., we "decorate" each row with the actual sort key, |
---|
142 | // sort based on the sort keys, and then put the rows back in order |
---|
143 | // which is a lot faster because you only do getInnerText once per row |
---|
144 | row_array = []; |
---|
145 | col = this.sorttable_columnindex; |
---|
146 | rows = this.sorttable_tbody.rows; |
---|
147 | for (var j=0; j<rows.length; j++) { |
---|
148 | row_array[row_array.length] = [sorttable.getInnerText(rows[j].cells[col]), rows[j]]; |
---|
149 | } |
---|
150 | /* If you want a stable sort, uncomment the following line */ |
---|
151 | //sorttable.shaker_sort(row_array, this.sorttable_sortfunction); |
---|
152 | /* and comment out this one */ |
---|
153 | row_array.sort(this.sorttable_sortfunction); |
---|
154 | |
---|
155 | tb = this.sorttable_tbody; |
---|
156 | // gabriel |
---|
157 | memory = "null"; |
---|
158 | color = "even"; |
---|
159 | for (var j=0; j<row_array.length; j++) { |
---|
160 | // gabriel |
---|
161 | if ( memory != row_array[j][0] ) { |
---|
162 | memory = row_array[j][0]; |
---|
163 | color = color =="odd" ? "even" : "odd"; |
---|
164 | } |
---|
165 | row_array[j][1].className = color; |
---|
166 | // row_array[j][1].className = j % 2 ? "even" : "odd"; |
---|
167 | tb.appendChild(row_array[j][1]); |
---|
168 | } |
---|
169 | |
---|
170 | delete row_array; |
---|
171 | }); |
---|
172 | } |
---|
173 | } |
---|
174 | }, |
---|
175 | |
---|
176 | guessType: function(table, column) { |
---|
177 | // guess the type of a column based on its first non-blank row |
---|
178 | sortfn = sorttable.sort_alpha; |
---|
179 | for (var i=0; i<table.tBodies[0].rows.length; i++) { |
---|
180 | text = sorttable.getInnerText(table.tBodies[0].rows[i].cells[column]); |
---|
181 | if (text != '') { |
---|
182 | if (text.match(/^-?[£$€]?[\d,.]+%?$/)) { |
---|
183 | return sorttable.sort_numeric; |
---|
184 | } |
---|
185 | // check for a date: dd/mm/yyyy or dd/mm/yy |
---|
186 | // can have / or . or - as separator |
---|
187 | // can be mm/dd as well |
---|
188 | possdate = text.match(sorttable.DATE_RE) |
---|
189 | if (possdate) { |
---|
190 | // looks like a date |
---|
191 | first = parseInt(possdate[1]); |
---|
192 | second = parseInt(possdate[2]); |
---|
193 | if (first > 12) { |
---|
194 | // definitely dd/mm |
---|
195 | return sorttable.sort_ddmm; |
---|
196 | } else if (second > 12) { |
---|
197 | return sorttable.sort_mmdd; |
---|
198 | } else { |
---|
199 | // looks like a date, but we can't tell which, so assume |
---|
200 | // that it's dd/mm (English imperialism!) and keep looking |
---|
201 | sortfn = sorttable.sort_ddmm; |
---|
202 | } |
---|
203 | } |
---|
204 | } |
---|
205 | } |
---|
206 | return sortfn; |
---|
207 | }, |
---|
208 | |
---|
209 | getInnerText: function(node) { |
---|
210 | // gets the text we want to use for sorting for a cell. |
---|
211 | // strips leading and trailing whitespace. |
---|
212 | // this is *not* a generic getInnerText function; it's special to sorttable. |
---|
213 | // for example, you can override the cell text with a customkey attribute. |
---|
214 | // it also gets .value for <input> fields. |
---|
215 | |
---|
216 | hasInputs = (typeof node.getElementsByTagName == 'function') && |
---|
217 | node.getElementsByTagName('input').length; |
---|
218 | |
---|
219 | if (node.getAttribute("sorttable_customkey") != null) { |
---|
220 | return node.getAttribute("sorttable_customkey"); |
---|
221 | } |
---|
222 | else if (typeof node.textContent != 'undefined' && !hasInputs) { |
---|
223 | return node.textContent.replace(/^\s+|\s+$/g, ''); |
---|
224 | } |
---|
225 | else if (typeof node.innerText != 'undefined' && !hasInputs) { |
---|
226 | return node.innerText.replace(/^\s+|\s+$/g, ''); |
---|
227 | } |
---|
228 | else if (typeof node.text != 'undefined' && !hasInputs) { |
---|
229 | return node.text.replace(/^\s+|\s+$/g, ''); |
---|
230 | } |
---|
231 | else { |
---|
232 | switch (node.nodeType) { |
---|
233 | case 3: |
---|
234 | if (node.nodeName.toLowerCase() == 'input') { |
---|
235 | return node.value.replace(/^\s+|\s+$/g, ''); |
---|
236 | } |
---|
237 | case 4: |
---|
238 | return node.nodeValue.replace(/^\s+|\s+$/g, ''); |
---|
239 | break; |
---|
240 | case 1: |
---|
241 | case 11: |
---|
242 | var innerText = ''; |
---|
243 | for (var i = 0; i < node.childNodes.length; i++) { |
---|
244 | innerText += sorttable.getInnerText(node.childNodes[i]); |
---|
245 | } |
---|
246 | return innerText.replace(/^\s+|\s+$/g, ''); |
---|
247 | break; |
---|
248 | default: |
---|
249 | return ''; |
---|
250 | } |
---|
251 | } |
---|
252 | }, |
---|
253 | |
---|
254 | reverse: function(tbody) { |
---|
255 | // reverse the rows in a tbody |
---|
256 | newrows = []; |
---|
257 | for (var i=0; i<tbody.rows.length; i++) { |
---|
258 | newrows[newrows.length] = tbody.rows[i]; |
---|
259 | } |
---|
260 | for (var i=newrows.length-1; i>=0; i--) { |
---|
261 | tbody.appendChild(newrows[i]); |
---|
262 | } |
---|
263 | delete newrows; |
---|
264 | }, |
---|
265 | |
---|
266 | /* sort functions |
---|
267 | each sort function takes two parameters, a and b |
---|
268 | you are comparing a[0] and b[0] */ |
---|
269 | sort_numeric: function(a,b) { |
---|
270 | aa = parseFloat(a[0].replace(/[^0-9.-]/g,'')); |
---|
271 | if (isNaN(aa)) aa = 0; |
---|
272 | bb = parseFloat(b[0].replace(/[^0-9.-]/g,'')); |
---|
273 | if (isNaN(bb)) bb = 0; |
---|
274 | return aa-bb; |
---|
275 | }, |
---|
276 | sort_alpha: function(a,b) { |
---|
277 | if (a[0]==b[0]) return 0; |
---|
278 | if (a[0]<b[0]) return -1; |
---|
279 | return 1; |
---|
280 | }, |
---|
281 | sort_ddmm: function(a,b) { |
---|
282 | mtch = a[0].match(sorttable.DATE_RE); |
---|
283 | y = mtch[3]; m = mtch[2]; d = mtch[1]; |
---|
284 | if (m.length == 1) m = '0'+m; |
---|
285 | if (d.length == 1) d = '0'+d; |
---|
286 | dt1 = y+m+d; |
---|
287 | mtch = b[0].match(sorttable.DATE_RE); |
---|
288 | y = mtch[3]; m = mtch[2]; d = mtch[1]; |
---|
289 | if (m.length == 1) m = '0'+m; |
---|
290 | if (d.length == 1) d = '0'+d; |
---|
291 | dt2 = y+m+d; |
---|
292 | if (dt1==dt2) return 0; |
---|
293 | if (dt1<dt2) return -1; |
---|
294 | return 1; |
---|
295 | }, |
---|
296 | sort_mmdd: function(a,b) { |
---|
297 | mtch = a[0].match(sorttable.DATE_RE); |
---|
298 | y = mtch[3]; d = mtch[2]; m = mtch[1]; |
---|
299 | if (m.length == 1) m = '0'+m; |
---|
300 | if (d.length == 1) d = '0'+d; |
---|
301 | dt1 = y+m+d; |
---|
302 | mtch = b[0].match(sorttable.DATE_RE); |
---|
303 | y = mtch[3]; d = mtch[2]; m = mtch[1]; |
---|
304 | if (m.length == 1) m = '0'+m; |
---|
305 | if (d.length == 1) d = '0'+d; |
---|
306 | dt2 = y+m+d; |
---|
307 | if (dt1==dt2) return 0; |
---|
308 | if (dt1<dt2) return -1; |
---|
309 | return 1; |
---|
310 | }, |
---|
311 | |
---|
312 | shaker_sort: function(list, comp_func) { |
---|
313 | // A stable sort function to allow multi-level sorting of data |
---|
314 | // see: http://en.wikipedia.org/wiki/Cocktail_sort |
---|
315 | // thanks to Joseph Nahmias |
---|
316 | var b = 0; |
---|
317 | var t = list.length - 1; |
---|
318 | var swap = true; |
---|
319 | |
---|
320 | while(swap) { |
---|
321 | swap = false; |
---|
322 | for(var i = b; i < t; ++i) { |
---|
323 | if ( comp_func(list[i], list[i+1]) > 0 ) { |
---|
324 | var q = list[i]; list[i] = list[i+1]; list[i+1] = q; |
---|
325 | swap = true; |
---|
326 | } |
---|
327 | } // for |
---|
328 | t--; |
---|
329 | |
---|
330 | if (!swap) break; |
---|
331 | |
---|
332 | for(var i = t; i > b; --i) { |
---|
333 | if ( comp_func(list[i], list[i-1]) < 0 ) { |
---|
334 | var q = list[i]; list[i] = list[i-1]; list[i-1] = q; |
---|
335 | swap = true; |
---|
336 | } |
---|
337 | } // for |
---|
338 | b++; |
---|
339 | |
---|
340 | } // while(swap) |
---|
341 | } |
---|
342 | } |
---|
343 | |
---|
344 | /* ****************************************************************** |
---|
345 | Supporting functions: bundled here to avoid depending on a library |
---|
346 | ****************************************************************** */ |
---|
347 | |
---|
348 | // Dean Edwards/Matthias Miller/John Resig |
---|
349 | |
---|
350 | /* for Mozilla/Opera9 */ |
---|
351 | if (document.addEventListener) { |
---|
352 | document.addEventListener("DOMContentLoaded", sorttable.init, false); |
---|
353 | } |
---|
354 | |
---|
355 | /* for Internet Explorer */ |
---|
356 | /*@cc_on @*/ |
---|
357 | /*@if (@_win32) |
---|
358 | document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>"); |
---|
359 | var script = document.getElementById("__ie_onload"); |
---|
360 | script.onreadystatechange = function() { |
---|
361 | if (this.readyState == "complete") { |
---|
362 | sorttable.init(); // call the onload handler |
---|
363 | } |
---|
364 | }; |
---|
365 | /*@end @*/ |
---|
366 | |
---|
367 | /* for Safari */ |
---|
368 | if (/WebKit/i.test(navigator.userAgent)) { // sniff |
---|
369 | var _timer = setInterval(function() { |
---|
370 | if (/loaded|complete/.test(document.readyState)) { |
---|
371 | sorttable.init(); // call the onload handler |
---|
372 | } |
---|
373 | }, 10); |
---|
374 | } |
---|
375 | |
---|
376 | /* for other browsers */ |
---|
377 | window.onload = sorttable.init; |
---|
378 | |
---|
379 | // written by Dean Edwards, 2005 |
---|
380 | // with input from Tino Zijdel, Matthias Miller, Diego Perini |
---|
381 | |
---|
382 | // http://dean.edwards.name/weblog/2005/10/add-event/ |
---|
383 | |
---|
384 | function dean_addEvent(element, type, handler) { |
---|
385 | if (element.addEventListener) { |
---|
386 | element.addEventListener(type, handler, false); |
---|
387 | } else { |
---|
388 | // assign each event handler a unique ID |
---|
389 | if (!handler.$$guid) handler.$$guid = dean_addEvent.guid++; |
---|
390 | // create a hash table of event types for the element |
---|
391 | if (!element.events) element.events = {}; |
---|
392 | // create a hash table of event handlers for each element/event pair |
---|
393 | var handlers = element.events[type]; |
---|
394 | if (!handlers) { |
---|
395 | handlers = element.events[type] = {}; |
---|
396 | // store the existing event handler (if there is one) |
---|
397 | if (element["on" + type]) { |
---|
398 | handlers[0] = element["on" + type]; |
---|
399 | } |
---|
400 | } |
---|
401 | // store the event handler in the hash table |
---|
402 | handlers[handler.$$guid] = handler; |
---|
403 | // assign a global event handler to do all the work |
---|
404 | element["on" + type] = handleEvent; |
---|
405 | } |
---|
406 | }; |
---|
407 | // a counter used to create unique IDs |
---|
408 | dean_addEvent.guid = 1; |
---|
409 | |
---|
410 | function removeEvent(element, type, handler) { |
---|
411 | if (element.removeEventListener) { |
---|
412 | element.removeEventListener(type, handler, false); |
---|
413 | } else { |
---|
414 | // delete the event handler from the hash table |
---|
415 | if (element.events && element.events[type]) { |
---|
416 | delete element.events[type][handler.$$guid]; |
---|
417 | } |
---|
418 | } |
---|
419 | }; |
---|
420 | |
---|
421 | function handleEvent(event) { |
---|
422 | var returnValue = true; |
---|
423 | // grab the event object (IE uses a global event object) |
---|
424 | event = event || fixEvent(((this.ownerDocument || this.document || this).parentWindow || window).event); |
---|
425 | // get a reference to the hash table of event handlers |
---|
426 | var handlers = this.events[event.type]; |
---|
427 | // execute each event handler |
---|
428 | for (var i in handlers) { |
---|
429 | this.$$handleEvent = handlers[i]; |
---|
430 | if (this.$$handleEvent(event) === false) { |
---|
431 | returnValue = false; |
---|
432 | } |
---|
433 | } |
---|
434 | return returnValue; |
---|
435 | }; |
---|
436 | |
---|
437 | function fixEvent(event) { |
---|
438 | // add W3C standard event methods |
---|
439 | event.preventDefault = fixEvent.preventDefault; |
---|
440 | event.stopPropagation = fixEvent.stopPropagation; |
---|
441 | return event; |
---|
442 | }; |
---|
443 | fixEvent.preventDefault = function() { |
---|
444 | this.returnValue = false; |
---|
445 | }; |
---|
446 | fixEvent.stopPropagation = function() { |
---|
447 | this.cancelBubble = true; |
---|
448 | } |
---|
449 | |
---|
450 | // Dean's forEach: http://dean.edwards.name/base/forEach.js |
---|
451 | /* |
---|
452 | forEach, version 1.0 |
---|
453 | Copyright 2006, Dean Edwards |
---|
454 | License: http://www.opensource.org/licenses/mit-license.php |
---|
455 | */ |
---|
456 | |
---|
457 | // array-like enumeration |
---|
458 | if (!Array.forEach) { // mozilla already supports this |
---|
459 | Array.forEach = function(array, block, context) { |
---|
460 | for (var i = 0; i < array.length; i++) { |
---|
461 | block.call(context, array[i], i, array); |
---|
462 | } |
---|
463 | }; |
---|
464 | } |
---|
465 | |
---|
466 | // generic enumeration |
---|
467 | Function.prototype.forEach = function(object, block, context) { |
---|
468 | for (var key in object) { |
---|
469 | if (typeof this.prototype[key] == "undefined") { |
---|
470 | block.call(context, object[key], key, object); |
---|
471 | } |
---|
472 | } |
---|
473 | }; |
---|
474 | |
---|
475 | // character enumeration |
---|
476 | String.forEach = function(string, block, context) { |
---|
477 | Array.forEach(string.split(""), function(chr, index) { |
---|
478 | block.call(context, chr, index, string); |
---|
479 | }); |
---|
480 | }; |
---|
481 | |
---|
482 | // globally resolve forEach enumeration |
---|
483 | var forEach = function(object, block, context) { |
---|
484 | if (object) { |
---|
485 | var resolve = Object; // default |
---|
486 | if (object instanceof Function) { |
---|
487 | // functions have a "length" property |
---|
488 | resolve = Function; |
---|
489 | } else if (object.forEach instanceof Function) { |
---|
490 | // the object implements a custom forEach method so use that |
---|
491 | object.forEach(block, context); |
---|
492 | return; |
---|
493 | } else if (typeof object == "string") { |
---|
494 | // the object is a string |
---|
495 | resolve = String; |
---|
496 | } else if (typeof object.length == "number") { |
---|
497 | // the object is array-like |
---|
498 | resolve = Array; |
---|
499 | } |
---|
500 | resolve.forEach(object, block, context); |
---|
501 | } |
---|
502 | }; |
---|
503 | |
---|
504 | |
---|