The functionality of jQuery autocomplete is hindered when trying to utilize a remote data source

HTML:

<input type="text" id="shop-id">

JS:

$(document).ready(function(){

    $( "#shop-id" ).autocomplete({
        source: "/ticket/get_sids",
        select: function(event, ui){
          //...
        }
    });               

});

Encountering an unusual issue with autocomplete. When setting the source using a static variable like this:

    var data = ["0200","0032"];

    $( "#shop-id" ).autocomplete({
        source: "/ticket/get_sids"
    });

everything functions as expected.

However, when utilizing a dynamic source URL, the autocomplete does not appear to filter the search results properly. Regardless of the input text, autocomplete displays the entire source without any filtering. For example, typing "g" should not display 0200 and 0032 because "g" does not match anything in the source.

The dynamic source returns pure JSON data such as: ["0200","0032"]. This is generated by a PHP page:

return new Response(json_encode($data));

which outputs

["0200","0032"]

in the browser window.

Environment: jQuery 1.7.2 jQuery-Ui 1.8.2

Answer №1

Make sure to include the data parameter in order to send back the filter value correctly. Implement server-side filtering before returning the response data.

" When I type a "g," it shows results that it shouldn't because there is no match in the source."

$(document).ready(function(){

    $( "#shop-id" ).autocomplete({
        source: "/ticket/get_sids",
        select: function(event, ui){
          //...
        },
       data: { term: request.term   }  //"term" can be customized based on your server side requirements
        //...
     })
 });    

If you need more guidance, this answer on Stack Overflow might be helpful:

Answer №2

Autocomplete functionality in the jQuery UI library requires a String that points to a URL resource returning JSON data. This URL can be on the same domain or a different one, with JSONP support if necessary. The Autocomplete plugin does not perform result filtering itself; instead, it appends a "term" parameter to the URL for server-side scripts to use in filtering.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Update all items in the menu to be active, rather than only the chosen one

Here is the layout of my menu along with the jQuery code included below. The functionality is such that when I click on Home Page, its parent element (list item) receives an active class. Currently, when I am on the Home Page, the list item for Account Co ...

Identify modifications in the content within an iframe, where the content is constantly changing due to the use of

Alright, so I have a jQuery mobile enabled page that is being loaded inside an iFrame. Typically, if I want to detect changes in the content of an iFrame, I would use: $('iframe').load(function(){ //content changed! }); However, in this par ...

Issues with submitting a Django form using AJAX

I am experiencing an issue with the URL when trying to create a "like button" using AJAX. Whenever I click on the button, I receive a 404 error with the following URL: 'add_like'%20%%7D. The server console responds with "POST /questions/get/1/%7B ...

Transmit an Array using Ajax and retrieve it on an ASP Classic page

I am facing a challenge where I need to pass an array using AJAX on an ASP page. After trying to send it as GET method and checking the data being received, I noticed that only the LAST record of the array is being processed by ASP. How can I successfu ...

When retrieving data from a JSON file, only display the value instead of showing the entire

When a user tries to login, I have created a modal window. If the user enters incorrect credentials, it returns SignInStatus.Failure, which is fine. However, the page then refreshes and only displays the number "3" at the top of the page with no other cont ...

The issue with executing event.keyCode == 13 in Firefox remains unresolved

I've implemented a function that sends comments only when the "enter" key is pressed, but not when it's combined with the "shift" key: $(msg).keypress(function (e) { if (event.keyCode == 13 && event.shiftKey) { event.stopProp ...

Extract data from the Ajax database and automatically hide the "Load More" button when all items

Every time I fetch data from my MySQL database, I retrieve 5 items at once. $query = $pdo->prepare("SELECT * FROM names WHERE id < ? ORDER BY id DESC LIMIT 5"); $query->execute([$_POST["id"]]); while($row = $query -> fetch() ...

How to resize and center an image within a div element as they both scale proportionally

Can someone help me figure out how to center a resized image within its container, which should also be centered? I've been struggling with this for 7 hours and can't seem to get it right. Any assistance would be greatly appreciated :-) Here is ...

Sending a form using Ajax and jQuery

I have created a login form using ajax (jQuery), but I'm facing an issue where the user's email address is not remembered after submission. This means that every time the user logs in, they have to re-enter their full email address instead of it ...

Share JSON data across functions by calling a function

I am currently working on a project where I need to load JSON using a JavaScript function and then make the loaded JSON objects accessible to other functions in the same namespace. However, I have encountered some difficulties in achieving this. Even after ...

Is there a method in bootstrap that reveals the elements with the hidden class?

Currently, I am loading data into a bootstrap table on my JSP. The table class is hidden at the moment. After successfully uploading the data into the bootstrap table, I have implemented the following code: $(function() { var table = $('#Table') ...

Customize DataTables to show specific rows with unique cell content and styling

We are currently using the jQuery DataTables plug-in and have encountered some limitations. Despite overcoming two major challenges, we are now facing a third issue: Within our table, we need to include a distinct row that serves as a visual separator bet ...

Error encountered when using the $.post function

$(".eventer button[name=unique]").click(function() { console.log('button clicked'); thisBtn = $(this); parent = $(this).parent(); num = parent.data('num'); id = parent.data('id'); if(typeof num ! ...

What is the best way to pass an array to PHP and then display it in HTML?

After setting up a form with 3 select tags in my HTML, I attempted to create an array using jQuery and send it to PHP. The goal is to retrieve data from PHP and display it on my HTML page. Below is the code snippet: HTML code <form name="myform"> ...

Tips for incorporating a tooltip into a disabled selectpicker option

A selectpicker I have is filled with various languages, listed as `li` tags within a `ul` tag. You can see an example in the image provided below. https://i.stack.imgur.com/dsg66.png It's noticeable that the `+ Add Language` option in the selectpick ...

How to Use jQuery Slice to Display the Top N Items from a Dropdown Menu

How can I display only the top 10 results from multiple UL lists in my navigation? The code snippet provided below currently works for the first list, but how can I extend this functionality to all of the lists? $(document).ready(function() { var elem ...

When the down key is pressed in a textarea, choose the list item

I have HTML similar to the following <div class="row"> <textarea id="txtArea" ng-model="input" ng-change="handleOnChange(input);getSearchField(input);" ng-click="search(input)" ng-focus="search(input);" ...

Initiate a CSS animation only when a different animation has completed

I am trying to create a sequence of animations using 2 squares. I want the animation for the red square to start only after the blue square's animation is complete. How can I achieve this cycle of animations? .box { width: 100px; height: 1 ...

After all other content has been fully loaded, jQuery UI is then loaded onto

For a while now, I've been following a YouTuber's JQuery UI tutorial. Every time I refresh the page, my HTML loads first and then my CSS fills in, causing a momentary flash of HTML and CSS without the UI. Is this normal? Can it be prevented? Her ...

Retrieve the element that is currently being hovered over within the same nested selector

I am facing a challenge in selecting the currently hovered element with a nested selector. The ".frmElement" class is used as the selector. When I hover over the ".frmElement" element at a certain level, all the previous selector elements display the hover ...