From 982fc09908cdae6231fdd388dddc4bc97a60b3ab Mon Sep 17 00:00:00 2001 From: thomas girod <56346771+imperosol@users.noreply.github.com> Date: Fri, 10 Mar 2023 10:49:14 +0100 Subject: [PATCH] Repair NaN bug for autocomplete on counter click (#583) * Repair NaN bug for autocomplete on counter click --- counter/static/counter/js/counter_click.js | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/counter/static/counter/js/counter_click.js b/counter/static/counter/js/counter_click.js index 46f22e93..eee8c686 100644 --- a/counter/static/counter/js/counter_click.js +++ b/counter/static/counter/js/counter_click.js @@ -45,7 +45,6 @@ $(function () { const code_field = $("#code_field"); let quantity = ""; - let search = ""; code_field.autocomplete({ select: function (event, ui) { event.preventDefault(); @@ -56,13 +55,13 @@ $(function () { code_field.val(quantity + ui.item.value); }, source: function (request, response) { - // by the dark magic of JS, parseInt("123abc") === 123 - quantity = parseInt(request.term); - search = request.term.slice(quantity.toString().length) - let matcher = new RegExp($.ui.autocomplete.escapeRegex(search), "i"); - response($.grep(products_autocomplete, function (value) { + const res = /^(\d+x)?(.*)/i.exec(request.term); + quantity = res[1] || ""; + const search = res[2]; + const matcher = new RegExp($.ui.autocomplete.escapeRegex(search), "i" ); + response($.grep(products_autocomplete, function(value) { value = value.tags; - return matcher.test(value); + return matcher.test( value ); })); }, });