This repository has been archived by the owner on Feb 17, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 40
/
ddtf.js
125 lines (110 loc) · 3.47 KB
/
ddtf.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
(function($) {
$.fn.ddTableFilter = function(options) {
options = $.extend(true, $.fn.ddTableFilter.defaultOptions, options);
return this.each(function() {
if($(this).hasClass('ddtf-processed')) {
refreshFilters(this);
return;
}
var table = $(this);
var start = new Date();
$('th:visible', table).each(function(index) {
if($(this).hasClass('skip-filter')) return;
var selectbox = $('<select>');
var values = [];
var opts = [];
selectbox.append('<option value="--all--">' + $(this).text() + '</option>');
var col = $('tr:not(.skip-filter) td:nth-child(' + (index + 1) + ')', table).each(function() {
var cellVal = options.valueCallback.apply(this);
if(cellVal.length == 0) {
cellVal = '--empty--';
}
$(this).attr('ddtf-value', cellVal);
if($.inArray(cellVal, values) === -1) {
var cellText = options.textCallback.apply(this);
if(cellText.length == 0) {cellText = options.emptyText;}
values.push(cellVal);
opts.push({val:cellVal, text:cellText});
}
});
if(opts.length < options.minOptions){
return;
}
if(options.sortOpt) {
opts.sort(options.sortOptCallback);
}
$.each(opts, function() {
$(selectbox).append('<option value="' + this.val + '">' + this.text + '</option>')
});
$(this).wrapInner('<div style="display:none">');
$(this).append(selectbox);
selectbox.bind('change', {column:col}, function(event) {
var changeStart = new Date();
var value = $(this).val();
event.data.column.each(function() {
if($(this).attr('ddtf-value') === value || value == '--all--') {
$(this).removeClass('ddtf-filtered');
}
else {
$(this).addClass('ddtf-filtered');
}
});
var changeStop = new Date();
if(options.debug) {
console.log('Search: ' + (changeStop.getTime() - changeStart.getTime()) + 'ms');
}
refreshFilters(table);
});
table.addClass('ddtf-processed');
if($.isFunction(options.afterBuild)) {
options.afterBuild.apply(table);
}
});
function refreshFilters(table) {
var refreshStart = new Date();
$('tr', table).each(function() {
var row = $(this);
if($('td.ddtf-filtered', row).length > 0) {
options.transition.hide.apply(row, options.transition.options);
}
else {
options.transition.show.apply(row, options.transition.options);
}
});
if($.isFunction(options.afterFilter)) {
options.afterFilter.apply(table);
}
if(options.debug) {
var refreshEnd = new Date();
console.log('Refresh: ' + (refreshEnd.getTime() - refreshStart.getTime()) + 'ms');
}
}
if(options.debug) {
var stop = new Date();
console.log('Build: ' + (stop.getTime() - start.getTime()) + 'ms');
}
});
};
$.fn.ddTableFilter.defaultOptions = {
valueCallback:function() {
return encodeURIComponent($.trim($(this).text()));
},
textCallback:function() {
return $.trim($(this).text());
},
sortOptCallback: function(a, b) {
return a.text.toLowerCase() > b.text.toLowerCase();
},
afterFilter: null,
afterBuild: null,
transition: {
hide:$.fn.hide,
show:$.fn.show,
options: []
},
emptyText:'--Empty--',
sortOpt:true,
debug:false,
minOptions:2
}
})(jQuery);