Utilize the auto-complete feature to narrow down your options when filtering through a list

Check out this plunker that showcases a feature I am aiming to achieve.

My goal is to implement an auto-complete list for users to filter the table.

Currently, the filtering works smoothly while the user is typing. However, if the user selects an option from the auto-complete list, the filtering stops working.

Here's an example scenario:

  • The plunker displays a list of 30 badges that Stack Overflow users can earn.
  • I type 'Au' into the text box; the filter narrows down the list to two badges.
  • Next, I press the down arrow button and then hit enter.

  • 'Autobiographer' gets auto-completed in the textbox, but the list still shows only two items.

How can I ensure that selecting an auto-complete option also triggers the list to be filtered accordingly?

<html lang="en">

  <head>
    <meta charset="utf-8" />
    <title>jQuery UI Autocomplete - Default functionality</title>
    <script data-require="<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="45242b22302924376b2f3605746b766b7c">[email protected]</a>" data-semver="1.3.9" src="https://code.angularjs.org/1.3.9/angular.js"></script>
    <link rel="stylesheet" href="//code.jquery.com/ui/1.11.2/themes/smoothness/jquery-ui.css" />
    <script src="//code.jquery.com/jquery-1.10.2.js"></script>
    <script src="//code.jquery.com/ui/1.11.2/jquery-ui.js"></script>
    <script>
      var app = angular.module('app', []);

      app.factory('badges', ['$http', function($http) {
          return $http.get('https://api.stackexchange.com/2.2/badges?page=1&order=desc&sort=rank&site=stackoverflow')
            .success(function(data) {
              return data;
            })
            .error(function(err) {
              return err;
            });
      }]);

      app.factory('badgesagain', ['$http', function($http) {
          return $http.get('https://api.stackexchange.com/2.2/badges?page=1&order=desc&sort=rank&site=stackoverflow')
            .success(function(data) {
              return data;
            })
            .error(function(err) {
              return err;
            });
      }]);  

      app.controller('ctrl', ['$scope', 'badges', 'badgesagain', function($scope, badges, badgesagain){

          badgeNames = [];
          badges.then(function(response){

            for(var i=0; i < response.data.items.length; i++){
              badgeNames[i] = response.data.items[i].name;
            }

          });

          $scope.availableTags = badgeNames;
          $scope.complete = function () {
            console.log('running');
            $( "#tags" ).autocomplete({
              source: $scope.availableTags
            });
          };

          badges.success(function(data) {
            $scope.badgeList = data['items'];
          });

    }]);
  </script>
  </head>

  <body ng-app="app" ng-controller="ctrl">
    <div class="ui-widget">
      <label for="tags">Tags: </label>
      <input id="tags" ng-keyup="complete()" ng-model="c.name"/>
    </div>
    <ol>
      <li ng-repeat="badge in badgeList | filter:c ">
        {{ badge.name }}
      </li>
    </ol>

  <body ng-app="app" ng-controller="ctrl">
    <div class="ui-widget">
      <label for="tags">Tags: </label>
      <input id="tags" ng-keyup="complete()" ng-model="c.name"/>
    </div>
    <ol>
      <li ng-repeat="badge in badgeList | filter:c ">
        {{ badge.name }}
      </li>
    </ol>
  </body>
</html>
  </body>
</html>

Answer №1

Using a combination of Angular and jQuery can lead to complications.

Why not try using an Angular-based solution such as allmighty-autocomplete.

Answer №2

It is generally not recommended to mix jQuery and Angular together for long-term projects as it can lead to issues where Angular may not detect changes made by jQuery plugins. A better approach would be to use an Angular plugin like the angular-bootstrap typeahead component.

However, if you are facing issues with integrating both frameworks, one way to fix this is by informing Angular about any selections made using a jQuery autocomplete plugin by utilizing the select callback.

Here is an example code snippet:

$scope.complete = function () {
  console.log('running');
  $( "#tags" ).autocomplete({
    source: $scope.availableTags,
    select : function(evt, ui) {
      $scope.$apply(function() {
        $scope.c.name = ui.item.value;
      });
    }
  });
}; 

You can also check out the Plnkr demo for a live demonstration.

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

Guide to utilizing nested map functions in JavaScript to retrieve specific elements

I have two arrays: one is a simple array, and the other is an array of objects. Here are the arrays:- arr1=["aadhar", "address", "email", "mobile", "name", "pancard", "voterid"]; arr2=[ { "id": 21, "name": "johndoe", " ...

Ensuring a fixed table with vertical scrolling only, no horizontal scrolling, while scrolling in either direction

I'm working with an HTML fixed design that contains a center-aligned table with extensive data scrollable both vertically and horizontally. To address the issue of keeping column headers visible when scrolling vertically, I used jQuery's clone() ...

Tips for utilizing the 'map' function with nested arrays within an array (JSON)

Below is the JSON data provided: users = [{ "name": "alex", "id":123, "surname":"xx", tarriff: { "id":1, "name":"free" } }, { "name": "tom", "id":124, "surname":"henry", tarriff: { "id":1, "name": ...

Can JavaScript be used to retrieve values of items in neighboring table rows?

I currently have items displayed in a table setup like this: <td></td> <td><input class="form-control" value="$title"></td> <td><textarea name="Text1" cols="40" rows="5">$tdesc</textarea><td> <td> ...

Web API Implementation of Null-Safe Empty List Handling

I have a scenario where an angular controller is making a POST request to a web API method. I attempted to implement null safety with the IEnumerable, but it ended up causing the IEnumerable to always be empty. Here is the angular call: $http.post(custom ...

Sum of Radio Selections

I currently have a form that allows users to select certain radio buttons, but if certain radio buttons are selected, others cannot be. To demonstrate this visually, I have created a JSFIDDLE. Please note that the code may appear a little messy. The issu ...

What could be causing the issue with my validation for alphabetical input?

I am currently working on a registration form that only accepts alphabetical input. However, I am facing an issue where my error message appears regardless of whether I input an alphabetical or special character. According to my understanding, the code sho ...

Unlocking the potential of AngularJS by circumventing the $http promises object management system

I have a service set up like this: angular.module('module') .factory('UserList', function ($http) { return {getUserList: $http.get('/portal-services/NemesysPortalBackend/rest/user/all')}; }); This forces me to use Use ...

Searching for a JavaScript button via aria-label and interacting with it using Python Selenium

I am currently utilizing Python Selenium to locate and interact with a specific javascript button on a webpage. The solutions provided in various forums do not suit my needs as the button I am targeting is unique. To begin, I will display an image of the p ...

using an array as an argument in the filtering function

Is there a way to pass an array to the filter method in JavaScript? I have successfully filtered an array using another array. However, my filter array currently has a global scope. How can I pass the array to make my code cleaner? var array = [1, 2, 3, ...

Guide on sending a request to an API and displaying the retrieved information within the same Express application

I recently developed a basic express app with API and JWT authentication. I am now attempting to enhance the app by incorporating page rendering through my existing /api/.. routes. However, I am facing challenges in this process. app.use('/', en ...

Having trouble looping through Html5 canvas with JavaScript in Rails?

I am working on a rails project where I have a model called microposts: class Micropost < ActiveRecord::Base attr_accessible :content, :avatar belongs_to :user mount_uploader :avatar, ImageUploader To display the microposts, I created a partial ...

Ensuring no null objects are present in the jQuery each function

In order to iterate through each key value pair in a JSON element and display it, I am encountering an issue where some of the keys have null values. As a result, using $.each is throwing an error: TypeError: obj is null I do not want to remove the null ...

Utilize NgRepeat to access an unidentified array in AngularJS

In a complex multi-level array, there are objects nested at the deepest level. [ [ [ "FUND", { "totassets":10.9, "totdate":"2015-03-23", "expratiogross":1.35, "exprationet" ...

Magento issue with unresponsive image map functionality

My website is built using the Ultimo theme in Magento, and I have implemented a script from to make the image map responsive. Here is the HTML code: <img name="sale" src="sale_1.jpg" width="1180" height="200" id="sale" usemap="#m_sale" alt="" />< ...

Storing the .NET Framework viewmodel in its own individual Javascript file

I have a question about structuring my design for SoC (Separation of Concerns). I currently have the following files: testController.cs testViewModel.cs testView.cshtml testScript.js Currently, I generate data in the controller, populate the testViewMod ...

The functionality of Jade views is disrupted by external CSS files

I am new to node.js and apologize if this question seems too obvious. I have built my app logic with all the routes and views, and it was working fine until I tried to add more styles to my views using Jade in Express. The only change I made was including ...

Cycle through the list and populate the table with the data

My attempt to clarify this explanation is my best, as articulating exactly what I am trying to achieve is quite challenging: Initially, I have a list of names: { "Items": [ { "Id": 0, "Name": "Robinson" }, ...

Working with a data variable in a jQuery AJAX operation

I've been attempting to utilize the following code, but it's not functioning as expected: UPDATED WORKING: $(document).ready(function() { $('.infor').click(function () { var datasend = $(this).html(); $.ajax({ ...