0

I need to set a search bar in my code where it will retrive the data of the products present in the database.Here in the same project they have used the exact code to do the product search I used that in my page but the search function is activating and the list of items are given below the search box but when i try to click on the required product the success message is trigerring as much times of the product that i have filtered. For example milk is the product i have searched and i get four result to the that product search. When i click on one product the success in the autocomplete is called four times.And after clearing and again typing milk and try to click on a product the same four products apppear but this time the success method is called 8 times. I am little confused how to handle this. in the previous refrence also the same problem occurs but they have handle it with the if condition to avoid the multiple time calling the product appending function. I am in search of optimizing only calling the function one time. Can Anyone help me through it.

//html
<input type="text" class="form-control" id="search_product"  onkeyup="autocomplete_product();" placeholder="Product Code, Product Description">
//js
var autocomplete_product = function() { 
    var sort_by_product = "search_product";
    contactInvalid = true;

    if (contactInvalid == true) {
        $('#search_product').autocomplete($('#base_path').val() + "/CbManager/searchProducts/" + sort_by_product, {
            minChars: 1,
            cacheLength: false,
            autoFill: false,
            scrollHeight: 900,
            scroll: true,
        }).result(function(event, data, formatted) {
            var result = data.toString();
            if (result != "No Records.") {
                result = result.split(",");
                var len = result.length;
                if (product_id == 0) {
                    ajaxProductsId(result[len - 1]);
                }
            }
        });
    }
};
autocomplete_product();

This is the refrence code i am talking about.I have tryed to make many corrections using chatgpt and also referring the autocomplete documentation like using source method to get the result the source method itself not invoking jQuery Autocomplete plugin 1.2.3 this is the version i am using to autocomplete

4
  • Welcome to Stack Overflow. You have assigned Autocomplete in both your Script code and also in your KeyUp event. This might explain why it occurring multiple times. Also it is not a good widget to initialize within a function. Commented Jul 20, 2024 at 14:33
  • Eventhough i use only one function that is keyup and writing the autocomplete_product as direct function the sucess function is triggered multiple times here Commented Jul 22, 2024 at 4:28
  • I would try to not use keyup at all. Autocomplete can be triggered to start searched with 0 characters. It's not clear why you are using that event to trigger the initialization of autocomplete. Commented Jul 22, 2024 at 14:58
  • Also, from your post it is not clear if you are using the jQuery UI Autocomplete or some other library. Commented Jul 22, 2024 at 15:03

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.