forked from simonexmachina/datatables-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataTables-tools.js
More file actions
218 lines (218 loc) · 6.96 KB
/
dataTables-tools.js
File metadata and controls
218 lines (218 loc) · 6.96 KB
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
(function($) {
$.fn.dataTablesTools = {
defaults: {
bootstrap: false,
jQueryUI: false,
columnFiltering: false,
selectableRows: false,
selectAll: true,
onDraw: null,
/**
* addUnloadHandler: Requires simonwade/form-unload.js.
* You can provide options that will be passed to formUnload().
*/
addUnloadHandler: null
}
};
$.fn.initDataTables = function( opts ) {
var options = $.extend($.fn.dataTablesTools.defaults, opts),
$elements = this;
// handle any dataTables
var dataTableInit = $.extend({
bJQueryUI: options.jQueryUI,
aaSorting: [],
fnDrawCallback: function() {
$(this).trigger({type: 'draw'});
}
}, opts),
customDataTableInit = $elements.data('data-table-init');
if( customDataTableInit != undefined ) {
$.extend(dataTableInit, customDataTableInit);
}
if( dataTableInit.sAjaxSource ) {
$.extend(dataTableInit, {
bServerSide: true,
bProcessing: true,
sPaginationType: "full_numbers"
});
}
if( options.bootstrap ) {
$elements
.addClass('table-striped table-bordered');
$.extend($.fn.dataTableExt.oStdClasses, {
'sWrapper': 'dataTables_wrapper form-inline'
});
}
if( options.onDraw ) {
$elements.on('draw', options.onDraw);
}
if( options.addUnloadHandler ) {
dataTableInit.fnServerData = function(sSource, aoData, fnCallback, oSettings) {
var formUnload = $(this).closest('form').data('formUnload');
if( !formUnload || formUnload.confirmUnload() ) {
$.fn.DataTable.defaults.fnServerData.call(this, sSource, aoData, fnCallback, oSettings);
}
else {
$(this).closest('.dataTables_wrapper')
.find('.dataTables_processing').css('visibility', 'hidden');
}
};
}
$elements
.not('.noDataTable')
.dataTablePrepare()
.dataTable(dataTableInit)
// handle hidden columns
.each(function() {
var $table = $(this), offset = 0;
$table.css('visibility', 'visible').css('height', 'auto');
$('thead th.hiddenColumn', $table).each(function() {
$table.dataTable().fnSetColumnVis(
$('thead th', $table).index(this) + offset++, false
);
});
});
// column filtering support
$elements.filter(options.columnFiltering ? '*' : '.dataTableColumnFiltering')
.dataTableColumnFiltering(options);
// onbeforeunload handling support
if( options.addUnloadHandler ) {
var unloadOptions = $.extend(
{}, (typeof options.addUnloadHandler == 'object' ? options.addUnloadHandler : {})
),
exclude = '.dataTables_filter input';
if( typeof unloadOptions.exclude == 'object' ) {
unloadOptions.exclude = unloadOptions.exclude.add($elements.find(exclude));
}
else {
unloadOptions.exclude = (unloadOptions.exclude ? unloadOptions.exclude + ', ' + exclude : exclude);
}
$elements.each(function() {
$(this).closest('form').formUnload(unloadOptions);
});
if( !unloadOptions.skipInit ) {
$elements.on('init', function() {
$(this).formUnload('addInputs', $($(this).dataTable().fnGetNodes()));
});
}
$elements.on('draw', function(e, dataTable, json) {
$(this).closest('form').formUnload('refresh');
});
}
// selectable rows support
$elements.filter(options.selectableRows ? '*' : '.dataTableSelectable')
// if we're loading from an AJAX source then we need to listen for the init event
.on('init', function() {
$(this).dataTableSelectable(options);
});
// if we're not loading from an AJAX source the 'init' event never gets fired
// by dataTables, so trigger it to normalise the interface
$elements.each(function() {
if( !dataTableInit.sAjaxSource ) {
$(this).trigger({type: 'init'});
}
});
return $elements;
};
$.fn.dataTablePrepare = function(options) {
return this.each(function() {
var $table = $(this);
if( $('thead', $table).length == 0 ) {
$('tbody', $table).before('<thead></thead>');
$('thead', $table).append($('tr:first', $table));
}
$table.addClass('dataTable');
$('.orderBySelection', $table.parentNode).hide();
});
};
$.fn.dataTableColumnFiltering = function(options) {
return this.each(function() {
var $table = $(this),
$row = $('tfoot tr', $table), $cell;
if( $row.length == 0 ) {
$table.append('<tfoot><tr></tr></tfoot>');
$row = $('tfoot tr', $table);
}
$('thead td, thead th', $table).each(function() {
$row.append('<td>'
+ ($(this).hasClass('noFiltering') ? ''
: '<input type="text" placeholder="Filter ' + $(this).text() + '" class="placeholder" tabindex="-1" />')
+ '</td>'
);
$('td:last input', $row).keyup(function () {
$table.dataTable().fnFilter(
this.value,
// Filter on the column (the index) of this element
$('td', $row).index(this.parentNode)
);
});
});
});
};
$.fn.dataTableSelectable = function( options ) {
return this.each(function() {
var $table = $(this).dataTable(), $row, checked, i;
var handler = function(e, loading) {
var $checkbox = $('input[type=checkbox]', this);
// clicking in an input or a link doesn't select the row
if( e && $(e.target).is('input, textarea, a') && e.target != $checkbox.get(0) ) {
return;
}
// if the row was clicked then toggle the checkbox
if( e && !$checkbox.is(e.target) ) {
checked = $checkbox.prop('checked', !$checkbox.prop('checked'));
$checkbox.trigger('change', e.target);
}
else { // it's already been toggled
checked = $checkbox.prop('checked') || false;
}
$(this).toggleClass('row_selected', checked);
if( !loading ) {
$table.trigger('dataTableSelection', [$table.dataTableNumSelected()]);
}
};
$($table.fnGetNodes()).each(function() {
handler.call(this, null, true);
});
$(this).on('click', '.dataTable tbody tr', function(e) {
handler.call(this, e);
});
$(this).on('change', '.dataTable input[type=checkbox]', function(e) {
handler.call($(this).closest('tr'), e);
});
$table.trigger('dataTableSelection', [$table.dataTableNumSelected()]);
$table.parents().filter('form').submit(function() {
$(this).append(
// create a hidden div
$(document.createElement('DIV')).hide()
// append all the checkboxes to this so the off-page ones are included
.append($($table.fnGetNodes()).find('input'))
); // and add this to the form
});
if( options.selectAll ) {
$table.before('<div class="dataTables_selection"><a class="link all">Select all</a> <a class="link none">Deselect all</a></div>');
$('a.all, a.none', $table.parent()).click(function() {
$link = $(this);
var filtered = $table.fnSettings().aiDisplay,
rows = [], i;
for( i = 0; i < filtered.length; i++ ) {
rows.push($table.fnGetNodes(filtered[i]));
}
var checked = $link.hasClass('all');
$(rows).each(function() {
$('input[type=checkbox]', this).each(function() {
$(this).prop('checked', checked);
$(this).trigger('change');
});
$(this).toggleClass('row_selected', checked);
});
$table.trigger('dataTableSelection', [$table.dataTableNumSelected()]);
});
}
});
};
$.fn.dataTableNumSelected = function() {
var $table = $(this).dataTable(), $row, checked, i;
return $(':checked', $table.fnGetNodes()).length;
};
})(jQuery);