Skip to content

Instantly share code, notes, and snippets.

@troygnichols
Created February 16, 2015 16:20
Show Gist options
  • Select an option

  • Save troygnichols/97d3c587e9961496a2ee to your computer and use it in GitHub Desktop.

Select an option

Save troygnichols/97d3c587e9961496a2ee to your computer and use it in GitHub Desktop.
function datatable(el, opts, callback) {
if (typeof(el) === 'undefined' || null === el) return;
var dt = $(el),
inputFilters = dt.find("tfoot input"),
selectFilters = dt.find("tfoot select"),
dateFilters = dt.find(".datepicker"),
allFilters = dt.find("tfoot input,select");
var defaultOpts = {
sPaginationType: "full_numbers",
bJQueryUI: true,
};
if (typeof opts == 'object') {
opts = $.extend(defaultOpts, opts);
} else {
opts = defaultOpts;
}
dt.dataTable(opts);
var delayedSearches = {};
var search = function(searchField, options) {
var field = $(searchField);
var index = allFilters.index(field);
var key = dt.prop('id') + '-' + index;
options = options || {};
var delayMillis = options.delayMillis || 0;
if (delayedSearches[key]) {
clearTimeout(delayedSearches[key]);
}
delayedSearches[key] = setTimeout(function() {
dt.fnFilter(field.val(), index);
delayedSearches[key] = null;
}, delayMillis);
};
var IGNORED_KEYCODES = [9,16];
inputFilters.keyup(function(event) {
if ( $.inArray(event.which, IGNORED_KEYCODES ) >= 0) { return; }
search(this, { delayMillis: 300 });
});
selectFilters.change(function() {
search(this);
});
dateFilters.change(function() {
search(this);
});
if (typeof callback == 'function') {
callback(dt);
}
return dt;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment