1/* 2 A simple, lightweight jQuery plugin for creating sortable tables. 3 https://github.com/kylefox/jquery-tablesort 4 Version 0.0.2 5*/ 6 7$(function() { 8 9 var $ = window.jQuery; 10 11 $.tablesort = function ($table, settings) { 12 var self = this; 13 this.$table = $table; 14 this.$thead = this.$table.find('thead'); 15 this.settings = $.extend({}, $.tablesort.defaults, settings); 16 this.$table.find('th').bind('click.tablesort', function() { 17 self.sort($(this)); 18 }); 19 this.index = null; 20 this.$th = null; 21 this.direction = null; 22 }; 23 24 $.tablesort.prototype = { 25 26 sort: function(th, direction) { 27 var start = new Date(), 28 self = this, 29 table = this.$table, 30 rows = this.$thead.length > 0 ? table.find('tbody tr') : table.find('tr').has('td'), 31 cells = table.find('tr td:nth-of-type(' + (th.index() + 1) + ')'), 32 sortBy = th.data().sortBy, 33 sortedMap = []; 34 35 var unsortedValues = cells.map(function(idx, cell) { 36 if (sortBy) 37 return (typeof sortBy === 'function') ? sortBy($(th), $(cell), self) : sortBy; 38 return ($(this).data().sortValue != null ? $(this).data().sortValue : $(this).text()); 39 }); 40 if (unsortedValues.length === 0) return; 41 42 self.$table.find('th').removeClass(self.settings.asc + ' ' + self.settings.desc); 43 44 if (direction !== 'asc' && direction !== 'desc') 45 this.direction = this.direction === 'asc' ? 'desc' : 'asc'; 46 else 47 this.direction = direction; 48 49 direction = this.direction == 'asc' ? 1 : -1; 50 51 self.$table.trigger('tablesort:start', [self]); 52 self.log("Sorting by " + this.index + ' ' + this.direction); 53 54 for (var i = 0, length = unsortedValues.length; i < length; i++) 55 { 56 sortedMap.push({ 57 index: i, 58 cell: cells[i], 59 row: rows[i], 60 value: unsortedValues[i] 61 }); 62 } 63 64 sortedMap.sort(function(a, b) { 65 if (a.value > b.value) { 66 return 1 * direction; 67 } else if (a.value < b.value) { 68 return -1 * direction; 69 } else { 70 return 0; 71 } 72 }); 73 74 $.each(sortedMap, function(i, entry) { 75 table.append(entry.row); 76 }); 77 78 th.addClass(self.settings[self.direction]); 79 80 self.log('Sort finished in ' + ((new Date()).getTime() - start.getTime()) + 'ms'); 81 self.$table.trigger('tablesort:complete', [self]); 82 }, 83 84 log: function(msg) { 85 if(($.tablesort.DEBUG || this.settings.debug) && console && console.log) { 86 console.log('[tablesort] ' + msg); 87 } 88 }, 89 90 destroy: function() { 91 this.$table.find('th').unbind('click.tablesort'); 92 this.$table.data('tablesort', null); 93 return null; 94 } 95 96 }; 97 98 $.tablesort.DEBUG = false; 99 100 $.tablesort.defaults = { 101 debug: $.tablesort.DEBUG, 102 asc: 'sorted ascending', 103 desc: 'sorted descending' 104 }; 105 106 $.fn.tablesort = function(settings) { 107 var table, sortable, previous; 108 return this.each(function() { 109 table = $(this); 110 previous = table.data('tablesort'); 111 if(previous) { 112 previous.destroy(); 113 } 114 table.data('tablesort', new $.tablesort(table, settings)); 115 }); 116 }; 117 118});
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.