/
jquery.tablesort.js
148 lines (119 loc) · 3.17 KB
/
jquery.tablesort.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
A simple, lightweight jQuery plugin for creating sortable tables.
https://github.com/kylefox/jquery-tablesort
Version 0.0.1
*/
$(function() {
var $ = window.jQuery;
function sortValueForCell(th, td, sorter) {
if(th.data().sortBy) {
var sortBy = th.data().sortBy;
return (typeof sortBy === 'function') ? sortBy(th, td, sorter) : sortBy;
}
if(td.data().sortValue) {
return td.data().sortValue;
} else {
return td.text();
}
}
$.tablesort = function ($table, settings) {
var self = this;
this.$table = $table;
this.$thead = this.$table.find('thead');
this.settings = $.extend({}, $.tablesort.defaults, settings);
this.$table.find('th').bind('click.tablesort', function() {
self.sort($(this));
});
this.index = null;
this.$th = null;
this.direction = null;
};
$.tablesort.prototype = {
sort: function(th, direction) {
var start = new Date(),
self = this,
table = this.$table,
rows = this.$thead.length > 0 ? table.find('tbody tr') : table.find('tr').has('td'),
aRow,
bRow,
aIndex,
bIndex,
cache = [];
if(rows.length === 0)
return;
self.$table.find('th').removeClass(self.settings.asc + ' ' + self.settings.desc);
this.index = th.index();
this.$th = th;
if(direction !== 'asc' && direction !== 'desc')
this.direction = this.direction === 'asc' ? 'desc' : 'asc';
else
this.direction = direction;
direction = this.direction == 'asc' ? 1 : -1;
self.$table.trigger('tablesort:start', [self]);
self.log("Sorting by " + this.index + ' ' + this.direction);
rows.sort(function(a, b) {
aRow = $(a);
bRow = $(b);
aIndex = aRow.index();
bIndex = bRow.index();
// Sort value A
if(cache[aIndex]) {
a = cache[aIndex];
} else {
a = sortValueForCell(th, self.cellToSort(a), self);
cache[aIndex] = a;
}
// Sort Value B
if(cache[bIndex]) {
b = cache[bIndex];
} else {
b = sortValueForCell(th, self.cellToSort(b), self);
cache[bIndex]= b;
}
if(a > b) {
return 1 * direction;
} else if(a < b) {
return -1 * direction;
} else {
return 0;
}
});
rows.each(function(i, tr) {
table.append(tr);
});
th.addClass(self.settings[self.direction]);
self.log('Sort finished in ' + ((new Date()).getTime() - start.getTime()) + 'ms');
self.$table.trigger('tablesort:complete', [self]);
},
cellToSort: function(row) {
return $($(row).find('td').get(this.index));
},
log: function(msg) {
if(($.tablesort.DEBUG || this.settings.debug) && console && console.log) {
console.log('[tablesort] ' + msg);
}
},
destroy: function() {
this.$table.find('th').unbind('click.tablesort');
this.$table.data('tablesort', null);
return null;
}
};
$.tablesort.DEBUG = false;
$.tablesort.defaults = {
debug: $.tablesort.DEBUG,
asc: 'sorted ascending',
desc: 'sorted descending'
};
$.fn.tablesort = function(settings) {
var table, sortable, previous;
return this.each(function() {
table = $(this);
previous = table.data('tablesort');
if(previous) {
previous.destroy();
}
table.data('tablesort', new $.tablesort(table, settings));
});
};
});