Created
February 16, 2015 16:20
-
-
Save troygnichols/97d3c587e9961496a2ee to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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