From 857637ddb4dddece39b2e319cbd432626e1ac952 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Wed, 8 Nov 2023 22:23:20 +0100 Subject: [PATCH] Bug 35284: Add DT delay to kohaTable To test: * When a specific initComplete exists (general patron search) * When no specific initComplete exists (cities) => KO * ERM tables display twice the "show X entries" dropdown list * Column filters are not affected Signed-off-by: David Cook Signed-off-by: Marcel de Rooy Signed-off-by: Tomas Cohen Arazi (cherry picked from commit 3819f4a57c9c208746e7f147ab085c36ccaf7d68) Signed-off-by: Fridolin Somers --- koha-tmpl/intranet-tmpl/prog/js/datatables.js | 39 ++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/koha-tmpl/intranet-tmpl/prog/js/datatables.js b/koha-tmpl/intranet-tmpl/prog/js/datatables.js index ee34580112..43b88fe2c4 100644 --- a/koha-tmpl/intranet-tmpl/prog/js/datatables.js +++ b/koha-tmpl/intranet-tmpl/prog/js/datatables.js @@ -51,7 +51,7 @@ var dataTablesDefaults = { "lengthMenu": [[10, 20, 50, 100, -1], [10, 20, 50, 100, __('All')]], "pageLength": 20, "fixedHeader": true, - initComplete: function( settings) { + initComplete: function( settings ) { var tableId = settings.nTable.id var state = settings.oLoadedState; state && toggledClearFilter(state.search.search, tableId); @@ -61,6 +61,10 @@ var dataTablesDefaults = { $(this).on( 'search.dt', function ( e, settings ) { toggledClearFilter(settings.oPreviousSearch.sSearch, tableId); }); + + if (settings.ajax) { + _dt_add_delay($("#"+tableId)); + } } }; @@ -917,6 +921,39 @@ function _dt_add_filters(table_node, table_dt, filters_options = {}) { } ); } +// List of unbind keys (Ctrl, Alt, Direction keys, etc.) +// These keys must not launch filtering +var blacklist_keys = new Array(0, 16, 17, 18, 37, 38, 39, 40); + +function _dt_add_delay(table_node, delay_ms) { + + let dt = table_node.DataTable(); + delay = (typeof delay == 'undefined') ? 500 : delay; + + var previousSearch = null; + var timerId = null; + $("#"+table_node.attr('id')+"_wrapper").find(".dataTables_filter input") + .unbind() + .bind("keyup", function(event) { + var input = $(this); + if (blacklist_keys.indexOf(event.keyCode) != -1) { + return; + } else if ( event.keyCode == '13' ) { + dt.search($(input).val()).draw(); + } else { + let val = $(input).val(); + if (previousSearch === null || previousSearch != val){ + window.clearTimeout(timerId); + previousSearch = val; + timerId = window.setTimeout(function(){ + dt.search($(input).val()).draw(); + }, delay); + } + } + + return; + }); +} (function($) { -- 2.39.5