-
Notifications
You must be signed in to change notification settings - Fork 754
/
jquery.tablesorter.widgets.js
265 lines (258 loc) · 8.31 KB
/
jquery.tablesorter.widgets.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/* TableSorter 2.0 Widgets - updated 11/7/2011
*
* jQuery UI Theme
* Column Styles
* Column Filters
* Sticky Header
* Column Resizing
*
*/
(function($){
// Add jQuery UI theme widget
// **************************
$.tablesorter.addWidget({
id: "uitheme",
format: function(table) {
var time, klass, rmv, c = table.config,
// ["up/down arrow (cssHeaders, unsorted)", "down arrow (cssDesc, descending)", "up arrow (cssAsc, ascending)" ]
icons = ["ui-icon-arrowthick-2-n-s", "ui-icon-arrowthick-1-s", "ui-icon-arrowthick-1-n"];
if (c.widgetUitheme && c.widgetUitheme.hasOwnProperty('css')) { icons = c.widgetUitheme.css || icons; }
rmv = icons.join(' ');
if (c.debug) {
time = new Date();
}
if (!$(table).is('.ui-theme')) {
$(table).addClass('ui-widget ui-widget-content ui-corner-all ui-theme');
$.each(c.headerList, function(){
$(this)
// using "ui-theme" class in case the user adds their own ui-icon using onRenderHeader
.addClass('ui-widget-header ui-corner-all')
.append('<span class="ui-icon"/>')
.hover(function(){
$(this).addClass('ui-state-hover');
}, function(){
$(this).removeClass('ui-state-hover');
});
});
}
$.each(c.headerList, function(i){
if (c.headers[i] && c.headers[i].sorter === false) {
// no sort arrows for disabled columns!
$(this).find('span.ui-icon').removeClass(rmv + ' ui-icon');
} else {
klass = ($(this).is('.' + c.cssAsc)) ? icons[1] : ($(this).is('.' + c.cssDesc)) ? icons[2] : $(this).is('.' + c.cssHeader) ? icons[0] : '';
$(this)[klass === icons[0] ? 'removeClass' : 'addClass']('ui-state-active')
.find('span.ui-icon').removeClass(rmv).addClass(klass);
}
});
if (c.debug) {
$.tablesorter.benchmark("Applying uitheme widget", time);
}
}
});
// Add Column styles widget
// **************************
$.tablesorter.addWidget({
id: "columns",
format: function(table) {
var $td, time, i, last, rmv,
c = table.config,
list = c.sortList,
len = list.length,
css = [ "primary", "secondary", "tertiary" ];
if (c.widgetColumns && c.widgetColumns.hasOwnProperty('css')) { css = c.widgetColumns.css || css; }
last = css.length-1;
rmv = css.join(' ');
if (c.debug) {
time = new Date();
}
// check if there is a sort (on initialization there may not be one)
if (list && list[0]) {
// loop through the visible rows
$("tr:visible", table.tBodies[0]).each(function (i) {
$td = $(this).children().removeClass(rmv);
// primary sort column class
$td.eq(list[0][0]).addClass(css[0]);
if (len > 1) {
for (i=1; i<len; i++){
// secondary, tertiary, etc sort column classes
$td.eq(list[i][0]).addClass( css[i] || css[last] );
}
}
});
}
if (c.debug) {
$.tablesorter.benchmark("Applying Columns widget", time);
}
}
});
// Add Filter widget
// **************************
$.tablesorter.addWidget({
id: "filter",
format: function(table) {
if (!table.config.filtering) {
var i, v, r, t, cr, icr, $td, c = table.config,
cols = c.headerList.length,
tbl = $(table),
fr = '<tr class="filters">',
time;
if (c.debug) {
time = new Date();
}
for (i=0; i < cols; i++){
fr += '<td><input type="text" class="filter" data-col="' + i + '" style="';
// use header option - headers: { 1: { filter: false } } OR add class="filter-false"
fr += ((c.headers[i] && 'filter' in c.headers[i] && c.headers[i].filter === false) || $(c.headerList[i]).is('.filter-false') ) ? 'display:none' : '';
fr += '"></td>';
}
tbl
.find('thead').append(fr += '</tr>')
.find('.filter').bind('keyup', function(e){
v = tbl.find('.filter').map(function(){ return ($(this).val() || '').toLowerCase(); }).get();
if (v.join('') === '') {
tbl.find('tr').show();
} else {
tbl.find('tbody').find('tr:not(.expand-child)').each(function(){
r = true;
cr = $(this).nextUntil('tr:not(.expand-child)');
// so, if icr (table.config.widgetFilterChildRows) is true and there is
// a match anywhere in the child row, then it will make the row visible
// checked here so the option can be changed dynamically
t = (cr.length && (typeof c.widgetFilterChildRows !== 'undefined' ? c.widgetFilterChildRows : true)) ? cr.text() : '';
$td = $(this).find('td');
for (i=0; i < cols; i++){
if (v[i] !== '' && ($td.eq(i).text() + t).toLowerCase().indexOf(v[i]) >= 0) {
r = (r) ? true : false;
} else if (v[i] !== '') {
r = false;
}
}
$(this)[r ? 'show' : 'hide']();
if (cr.length) { cr[r ? 'show' : 'hide'](); }
});
}
tbl.trigger('applyWidgets'); // make sure zebra widget is applied
});
c.filtering = true;
if (c.debug) {
$.tablesorter.benchmark("Applying Filter widget", time);
}
}
}
});
// Sticky header widget
// based on this awesome article:
// http://css-tricks.com/13465-persistent-headers/
// **************************
$.tablesorter.addWidget({
id: "stickyHeaders",
format: function(table) {
if ($(table).find('.stickyHeader').length) { return; }
var win = $(window),
header = $(table).find('thead'),
hdrCells = header.find('tr').children(),
brdr = parseInt(hdrCells.eq(0).css('border-left-width'),10),
sticky = header.find('tr').clone()
.addClass('stickyHeader')
.css({
width : header.outerWidth() + brdr * 2,
position : 'fixed',
top : 0,
marginLeft : -brdr,
visibility : 'hidden',
zIndex : 10
}),
stkyCells = sticky.children();
// update sticky header class names to match real header
$(table).bind('sortEnd', function(e,t){
var th = $(t).find('thead tr'),
sh = th.filter('.stickyHeader').children();
th.filter(':not(.stickyHeader)').children().each(function(i){
sh.eq(i).attr('class', $(this).attr('class'));
});
});
// set sticky header cell width and link clicks to real header
hdrCells.each(function(i){
var t = $(this),
s = stkyCells.eq(i)
// set cell widths
.width( t.width() )
// clicking on sticky will trigger sort
.bind('click', function(e){
t.trigger(e);
})
// prevent sticky header text selection
.bind('mousedown', function(){
this.onselectstart = function(){ return false; };
return false;
});
});
header.prepend( sticky );
// make it sticky!
win
.scroll(function(){
var $t = $(table),
offset = $t.offset(),
sTop = win.scrollTop(),
vis = ((sTop > offset.top) && (sTop < offset.top + $t.find('tbody').height())) ? 'visible' : 'hidden';
sticky.css('visibility', vis);
})
.resize(function(){
sticky.css({ width: header.outerWidth() + brdr * 2 });
stkyCells.each(function(i){
$(this).width( hdrCells.eq(i).width() );
});
});
}
});
// Add Column resizing widget
// **************************
$.tablesorter.addWidget({
id: "resizable",
format: function(table) {
if (!table.config.resizable) {
var i, w, c = table.config,
cols = c.headerList,
len = cols.length,
stopResize = function(){
c.resizable_position = 0;
c.resizable_target = null;
$(window).trigger('resize'); // will update stickyHeaders, just in case
};
c.resizable_target = null;
c.resizable_position = 0;
for (i=1; i < len; i++){
$(cols[i])
.append('<div class="resizer" style="cursor:w-resize;position:absolute;height:100%;width:20px;left:-20px;top:0;z-index:1;"></div>')
.wrapInner('<div style="position:relative;height:100%;width:100%"></div>')
.find('.resizer')
.bind('mousedown', function(e){
// save header cell and mouse position
c.resizable_target = $(e.target).closest('th');
c.resizable_position = e.pageX;
}).end()
.bind('mousemove', function(e){
// ignore mousemove if no mousedown
if (c.resizable_position === 0 || typeof(c.resizable_target) === null) { return; }
var w = e.pageX - c.resizable_position,
n = c.resizable_target.closest('th').prev();
// make sure
if ( c.resizable_target.width() < -w || ( n && n.width() <= w )) { return; }
// resize current column
n.width( n.width() + w );
c.resizable_position = e.pageX;
})
.bind('mouseup', function(){
stopResize();
return false;
});
}
$(table).find('thead').bind('mouseup mouseleave', function(){
stopResize();
});
c.resizable = true;
}
}
});
})(jQuery);