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 = ""; } ?> - +