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
keyupat 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.