From 85e626920fb4833e2eba42b0d812f623a6ac4a5a Mon Sep 17 00:00:00 2001 From: Brian Matovu Date: Sun, 19 Apr 2020 22:03:26 +0300 Subject: [PATCH] Update filter on return plugin **Fixes legacy API method `fnFilterOnReturn`** #354 --- api/filterOnReturn().js | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 api/filterOnReturn().js diff --git a/api/filterOnReturn().js b/api/filterOnReturn().js new file mode 100644 index 0000000..478a155 --- /dev/null +++ b/api/filterOnReturn().js @@ -0,0 +1,36 @@ +/** + * This plug-in removes the default behaviour of DataTables to filter on each + * keypress, and replaces with it the requirement to press the enter key to + * perform the filter. + * + * @name filterOnReturn + * @summary Require the return key to be pressed to filter a table + * @author [Brian Matovu](http://www.bmatovu.com) + * + * @returns {jQuery} jQuery instance + * + * @example + * $(document).ready(function() { + * var users_dt = $('table[id=tbl_users]').dataTable(); + * + * users_dt.filterOnReturn(); + * }); + */ + +jQuery.fn.dataTable.Api.register('filterOnReturn()', function() { + var dt = this; + + dt.settings().each(function (setting) { + $.each(setting.aanFeatures.f, function(idx, filter) { + var dtFilterInput = $('input', filter); + + dtFilterInput.unbind(); + + dtFilterInput.bind('keyup', function (event) { + if (event.keyCode == 13) { + dt.search(this.value).draw(); + } + }); + }); + }); +});