From f4d9dd7a919eb4b25fab3b822e56abdd8690bfd2 Mon Sep 17 00:00:00 2001 From: unfr Date: Mon, 11 Aug 2025 14:03:41 +0200 Subject: [PATCH] correction recherche ajax --- autopost/server.js | 43 +++++++++++++++++++++++++++++-------------- 1 file changed, 29 insertions(+), 14 deletions(-) diff --git a/autopost/server.js b/autopost/server.js index b114d25..9520b28 100644 --- a/autopost/server.js +++ b/autopost/server.js @@ -444,22 +444,37 @@ autopostRouter.get('/', async (req, res) => { $(document).ready(function(){ // Recherche AJAX + let searchTimer = null; + $("#searchInput").on("keyup", function() { - var q = $(this).val(); - $.ajax({ - url: '/autopost/search', - type: 'GET', - data: { q: q }, - dataType: 'json', - success: function(data) { - updateTable(data); - applyFilterAndSearch(); // conserve filtre actif - }, - error: function() { - alert("Erreur lors de la recherche."); - } - }); + clearTimeout(searchTimer); // annule le timer précédent + + let q = $(this).val(); + + searchTimer = setTimeout(function() { + $.ajax({ + url: '/autopost/search', + type: 'GET', + data: { q: q }, + dataType: 'json', + success: function(data) { + updateTable(data); + if (typeof currentFilter === 'undefined') { + currentFilter = null; + } + applyFilterAndSearch(); + }, + error: function(jqXHR, textStatus, errorThrown) { + // On ignore l'erreur si la requête a été annulée volontairement + if (textStatus !== "abort") { + console.error('Erreur AJAX :', textStatus, errorThrown); + alert("Erreur lors de la recherche."); + } + } + }); + }, 300); // 300ms après la dernière frappe }); + function toggleShowAllButton() { if (currentFilter === null) {