Creating an interactive dropdown menu using uib-typeahead directive in AngularJS

Managing a data application with large tables can be challenging.

We have tried local storage solutions to improve performance on slow connections, but we are encountering difficulties when attempting to implement custom filtering for typeaheads.

Even though the typeahead functions properly with local values, the sorting is not always accurate. This leads to frustration as users may struggle to find the correct match among hundreds of results.

After exploring various libraries, I have been unable to find a straightforward way to adjust the sorting method in Angular implementation. As a solution, we decided to dynamically generate the list based on user input using the following markup:

<input type="text" class="form-control"
    placeholder="Search"
    ng-model="oi.clinicalDisorder"
    typeahead-wait-ms="1000"
    uib-typeahead="clinicalDisorder as clinicalDisorder.clinicalDisorderName for clinicalDisorder in startsWith($viewValue)  | limitTo:200"
    typeahead-editable="false">

In my controller:

    $scope.startsWith = function(viewValue) {
            if (viewValue.length>3){
                return $http({
                      method: 'GET',
                      url: 'api/_search/clinical-disorders/'+viewValue})
                      .then(function successCallback(response) {
                          $scope.dynamicClinicalDisorders = response.data;
                          return $scope.dynamicClinicalDisorders;
                            },
                          function errorCallback(response) {
                                console.log(response);
                            });
                        }
                    };

The backend query is functioning correctly.

While the client-side code successfully accesses and prints the values from $scope.dynamicClinicalDisorders, it fails to load the list into the typeahead dropdown.

The foundation of my code can be found in the plunker shared in response to this question.

I am confident that there is a simple oversight causing this issue, but after spending considerable time on it, I seem to be missing it.

Answer №1

The autocomplete feature works well with a local array of values, but the sorting is not always accurate. This can be frustrating for users because sometimes the most obvious match appears last or gets buried among numerous other results.

This issue seems to be related to delays. When you display 100-200 items and the user enters new input during that time, it's possible that two calls are made in quick succession. As a result, the user may receive old results on the new call.

I recommend implementing a delay of 300ms (a standard typing delay) and using $http

config.timeout = cancelerEvent.promise
to cancel the old call if a new one is initiated.

I hope this solution proves helpful.


[EDIT]

In regards to the 300ms delay:

var timeoutHandlerFilterRowWatcher;

 if (timeoutHandlerFilterRowWatcher !== undefined) {
    $timeout.cancel(timeoutHandlerFilterRowWatcher);
  }

timeoutHandlerFilterRowWatcher = $timeout(function () {
      // perform your HTTP request
}, 300);

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

Implementing AngularJS ng-click to dynamically add a duplicate Div or Widget with every click

The code below showcases a series of divs displayed one after the other: <ul class="record"> <li id="{{record.id}}" ng-repeat="record in records"> <div> Widget Code.... </div> </li> </ul> In addition to th ...

An error occurred with redirecting using jQuery Mobile's new method in version 1.5

Utilizing jQuery Mobile for its history state feature, I am looking to redirect users to another "page" using the jQuery method recommended in their latest documentation. p/s: I prefer the jQuery navigation method due to the hashchange/history support and ...

Tips for executing unit tests using karma in a project upgrading from AngularJS version 1 to version 2

Currently, I am following the upgrading tutorial for angular-phonecat. I encountered an issue when trying to run unit tests after converting the Phone service to angular2. While the application itself is working fine, the tests are not running as expected. ...

Having trouble getting Firestore/Firebase to work with Next.js?

Hey everyone, I could really use some help here. I've been working on integrating Firebase into my Next.js app for the API. Everything works well when I build and run locally, but once I deploy to Vercel for production, I encounter a 500 - Internal S ...

Determining if a replacement took place in the Javascript replace function

I've developed a function that scans through a set of strings and replaces specific patterns: var content = 'Here is a sample string 1/23'; var currentDate = new Date(); var currentMonth = currentDate.getMonth()+1; var currentDay = curre ...

Shifting Icon to the Right within the Drawer Navigator Toolbar

While modifying the example code for Material UI's drawer navigator, I decided to enhance it by adding a notification icon and a checkout icon with the Admin Panel typography in the toolbar. However, I encountered an issue where the checkout icon app ...

Just getting started with JavaScript and running into trouble creating an array of image objects using an array of strings

I am struggling to accurately describe my issue, making it difficult to find a solution. I have an array called tempData generated from a text file that I want to reference with variables of the same name as the strings in the array. var red = new Image ...

Do not decode automatically, encode URI component

My challenge is dealing with users who have slashes in their usernames. I want to create easy-to-use URLs for them, like /user/username, even if the username contains special characters, such as /user/xXx/superboy. I'm utilizing client-side routing, ...

Creating a Set of Buttons in HTML

Need some assistance with grouped buttons. Let's consider a scenario where there are 5 buttons on an HTML page. When the 3rd button is clicked, buttons from 0 to 3 should change color and the function should return 3. Similarly, when the 5th button is ...

Reduce the size of several JavaScript files using webpack

Hello, I am a beginner with webpack and I am looking to minify all the files in a folder into a single js file using webpack. I want to avoid adding each file manually in the entry section. I believe there must be a more efficient way to achieve this, bu ...

Looking for a way to make this HTML tab appear using ng-show and an external variable in AngularJS - seeking guidance as a beginner!

Here is the HTML code snippet controlling the tab presentation: <li class="presentation" ng-show="currentUserAuthority == LIBRARIAN" > However, there seems to be an issue with the variable "currentUserAuthority" not updating until after the web pag ...

Navigating Through Grid and Card Content in React

My goal was to minimize the amount of code by incorporating a reusable component into my application. I am facing an issue on how to iterate through the columns and rows in Grid, Card, and Table. What would be the optimal solution for this problem? Please ...

Adjusting the Stripe Button to Utilize a Unique Button Class

I have a stripe button on my website and I want to add my custom class to it. I discovered that manually creating the CSS for it can work, but I prefer to maintain consistency across all buttons on my site by using my custom class. No matter what I attemp ...

The execution of form validation and any following code is halted

I have an update form that is called inside a modal on my main page. Whenever I click on it, an XMLHttpRequest triggers the edit page containing the form with the stored data values. Everything seems to work fine except for the form validation and AJAX use ...

Executing multiple asynchronous XMLHttpRequests with React

I experimented with multiple asynchronous XMLHttpRequests based on various examples I came across: var URL=["https://api.github.com/users/github","https://api.github.com/users/github/repos"]; var xhr = [null, null]; for (var i = 0; i < 2; i++) { ( ...

The function e.preventDefault() appears to be ineffective when applied to both the submit button and anchor tag within an

In my ASP.Net Core MVC App View <form> <div class="container"> <div class="row"> <div class="col-md-offset-2 col-md-4"> <div class="form-group"> <input type="text" class="form-contr ...

Setting the height of nested tabs within a parent tab in jQueryUI 2 does not work properly when the tabs are hidden

While developing my app, I encountered an issue with implementing main pages using jQueryUI tabs with heightStyle:"fill". The problem arises when one of these pages contains two more tab elements that should appear side-by-side. To achieve this layout, I w ...

Element.style prevents customization of Rev Slider dimensions

I'm currently attempting to adjust the height of my rev slider from 100% to 80%, but I can't seem to locate the necessary code. Upon inspecting in Chrome, I noticed the following: element.style { max-height: 1080px; margin-to ...

Using jQuery to retrieve the unique identifier of an element and checking if it matches a certain value before including the link

I'm currently using this code to retrieve values from a database and then add them to a form. The initial part appends the name of the Firewall and includes the unique ID in the element's id. if (value.type == 'Firewall') { $(' ...

My website code being stored in cache by Chrome (as well as other browsers)

Issue: I am facing an issue with hosting a widget on my client's website where the widget needs to be different for each page. To display the widget, the client embeds a script tag on their pages. This script is loaded on every page and the content ...