From a73628d769bd943319dcf9bf7c041a27b3741598 Mon Sep 17 00:00:00 2001 From: Tim Krehan Date: Tue, 21 May 2019 09:21:59 +0200 Subject: [PATCH] implemented autocomplete again --- bin/autocomplete.js | 34 ++++++++++++++++++++++++++++++++++ bin/manageRecipe.js | 11 ++--------- cont/manageRecipe.php | 11 +++++++++-- php/classes.recipe.php | 4 ++-- php/edit-recipes.php | 2 +- 5 files changed, 48 insertions(+), 14 deletions(-) create mode 100644 bin/autocomplete.js diff --git a/bin/autocomplete.js b/bin/autocomplete.js new file mode 100644 index 0000000..c211afa --- /dev/null +++ b/bin/autocomplete.js @@ -0,0 +1,34 @@ +function autocomplete() { + var apiurl = $(this).data("apiurl"); + var search = $(this).val(); + var elem = $(this); + if (search.length > $(this).data("strlen")) { + $.post({ + url: apiurl, + data: { + q: search + }, + success: function (data) { createAutocompleteDropdown(elem, JSON.parse(data)); } + }); + } +} +function createAutocompleteDropdown(elem, values) { + var dropdownmenue = $('[aria-labelledby=' + elem.attr("id") + ']'); + var dropdownItems = dropdownmenue.find(".dropdown-item"); + dropdownItems.remove(); + + for (index = 0; index < values.length; index++) { + var value = values[index]; + var regex = new RegExp((elem).val(), "ig"); + var displayvalue = value.replace(regex, ("" + (elem).val() + "")); + + var button = $(""); + button.click(dropdownButtonClick); + + dropdownmenue.append(button); + } +} +function dropdownButtonClick() { + var string = $(this).data("val"); + $('#' + $(this).attr("aria-textfield")).val(string); +} \ No newline at end of file diff --git a/bin/manageRecipe.js b/bin/manageRecipe.js index 0c31525..8eb1600 100644 --- a/bin/manageRecipe.js +++ b/bin/manageRecipe.js @@ -4,15 +4,7 @@ $(document).ready(function () { $("input[type=number]").on("focus", function () { $(this).select(); }); $("input[type=text]").on("focus", function () { $(this).select(); }); - // implement autocomplete with https://bootstrap-autocomplete.readthedocs.io/en/latest/# - $(".autocomplete-ingredient").autoComplete( - { - resolverSettings: { - url: '/api/recipes/auto' - } - } - ); - + $(".autocomplete-ingredient").on("input", autocomplete); }); function removeItem(elem) { @@ -33,6 +25,7 @@ function addItem() { clone.find("input[type=text]").attr("name", ("ingredient[" + dataID + "][Name]")); clone.find("input[type=text]").val(""); + clone.find("input[type=text]").on("input", autocomplete); clone.find("input[type=text]").on("focus", function () { $(this).select(); }); clone.find("input[type=number]").attr("name", ("ingredient[" + dataID + "][Amount]")); clone.find("input[type=number]").val("1"); diff --git a/cont/manageRecipe.php b/cont/manageRecipe.php index 580519a..3ded3fe 100644 --- a/cont/manageRecipe.php +++ b/cont/manageRecipe.php @@ -12,8 +12,8 @@ $additionalInput = ""; } ?> - +

@@ -54,12 +54,19 @@ } ?> +
- + + + +
+
diff --git a/php/classes.recipe.php b/php/classes.recipe.php index 7052adc..62ea486 100644 --- a/php/classes.recipe.php +++ b/php/classes.recipe.php @@ -175,10 +175,10 @@ $mysqli->close(); } - function getAllIngredients(){ + function getAllIngredientsContaining($q){ include $_SESSION["docroot"].'/php/connect.php'; $values = array(); - $result = $mysqli->query("SELECT Name FROM Zutat ORDER BY Name ASC"); + $result = $mysqli->query("SELECT Name FROM Zutat WHERE Name LIKE '%$q%' ORDER BY Name ASC"); while($item = $result->fetch_assoc()){ array_push($values, $item["Name"]); } diff --git a/php/edit-recipes.php b/php/edit-recipes.php index 54be070..f308471 100644 --- a/php/edit-recipes.php +++ b/php/edit-recipes.php @@ -13,7 +13,7 @@ break; case 'auto': - $book->getAllIngredients(); + $book->getAllIngredientsContaining($_POST["q"]); break; case 'edit':