angucomplete-alto automatically fills in data based on another input

Having two autocomplete select boxes with a unique feature has been quite interesting. The first input accepts a code that is related to a label, autofilling the second input with the corresponding object once the code is selected in the first input. However, the second input also offers autocomplete functionality as the code field is not mandatory.

An important detail about the first input (code) is that it always consists of 2 characters, no more and no less. Despite this, users can still input more than 2 characters. While my code effectively auto-selects the object and removes any extra characters beyond the required 2 from the user's input in the first field, I actually need those additional characters to remain. How can I customize this functionality?

The autocomplete module I am utilizing is Angucomplete-Alt.

Here's a snippet of my code:

<div angucomplete-alt
                                  id="flight_code"

                                  placeholder="flight code"
                                  pause="100"
                                  selected-object="claim.flight_details.flight_code"
                                  local-data="airlines"
                                  local-search="localSearch"
                                  search-fields="code_airline"
                                  title-field="code_airline"
                                  minlength="2"
                                  input-name="operating_airline"
                                  input-class="form-control form-control-small"
                                  match-class="highlight"
                                  field-required="false">

<div angucomplete-alt
                                   local-search="localSearch"
                                  id="operating_airline"
                                  placeholder="Search airline"
                                  pause="100"
                                  selected-object="claim.flight_details.operating_airline"
                                  local-data="airlines"
                                  search-fields="label"
                                  title-field="label"
                                  minlength="1"
                                  input-name="operating_airline"
                                  input-class="form-control form-control-small"
                                  match-class="highlight"
                                  field-required="true"
                                  initial-value="claim.flight_details.flight_code.originalObject">
                                </div>

Controller:

 $scope.localSearch = function(str, code_airline) {
  var matches = [];
  code_airline.forEach(function(code) {

      if(str.toString().substring(0, 2).toUpperCase() === code.code_airline){
          console.log("I found him!!");
          matches.push(code);
      }       

  });
  return matches;
};

Answer №1

I successfully resolved my issue by modifying the initial input code to a standard input and utilizing the ngChange directive to track changes in characters. I then created a promise to search for the corresponding object, which was subsequently inserted into the angcomplete input using the initialValue property.

Controller:

$scope.automaticFill = function(){
    var str = $scope.claim.flight_details.flight_code;
   if(str.toString().length === 2){
          console.log("Change detected");
       $http.get('data/airlines-companies.json').then(function(response){

        var airlines = response.data; 

            airlines.forEach(function(code) {
            if(code.code_airline === str.toString().toUpperCase())
             $scope.test = code;
            });
        });

      }
};    

HTML:

<input type="text" 
                                class="form-control" 
                                ng-model="claim.flight_details.flight_code" 
                                name="flight_code" 
                                id="flight_code"
                                ng-change="automaticFill()">

<div angucomplete-alt
                                   local-search="tap"
                                  id="operating_airline"
                                  placeholder="Search airline"
                                  pause="100"
                                  selected-object="claim.flight_details.operating_airline"
                                  local-data="airlines"
                                  search-fields="label"
                                  title-field="label"
                                  minlength="1"
                                  input-name="operating_airline"
                                  input-class="form-control form-control-small"
                                  match-class="highlight"
                                  field-required="true"
                                  initial-value="test">

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

My Express server is having trouble loading the Static JS

I'm feeling frustrated about this particular issue. The problem seems to be well-solved, and my code looks fine, but I can't figure out what's wrong . . . I have a JavaScript file connecting to my survey page, which I've added at the b ...

problem with the video pathway in the javascript document

I'm currently in the process of putting together a Video gallery playlist using HTML, CSS, and JavaScript. So far, I've set up the html and css files along with two js files. The first js file contains all the video information as shown here: ...

Using Node.js to parse JSON data fetched from the web

My current challenge involves retrieving and parsing JSON from a web API (https://api.coinmarketcap.com/v1/ticker/?limit=3) in order to extract the name and price_usd fields. For example: [ { ... sample data provided ... } ] The code snippet I am wo ...

The SyntaxError is triggered by the React-Native FlatList component

As a newcomer to React Native, I am facing an issue with refreshing the component to load city names stored in async storage. The error occurs when I utilize the FlatList component for the first time. The specific error message I encountered is: SyntaxE ...

Incorporate highcharts data into your Laravel project using the power of AJAX

Encountering an issue with loading data for a Highcharts chart. The response from my controller provides the following data: [['Doctorado', 91.86],['Maestría', 6.98],['Licenciatura', 1.16]] Although the AJAX call is succes ...

Using Selenium in Java to interact with popup elements

Attempting to retrieve and interact with pop-up/alert elements using selenium in Java has been a bit challenging for me. Below is the code snippet I have been working on: import org.openqa.selenium.By; import org.openqa.selenium.JavascriptExecutor; import ...

Angular: Discovering and retrieving all images within a div container

I am on a quest to locate all image tags within a specific div. These image tags are nested within paragraph tags inside the div. I attempted to create a plunkr to accomplish this task, but unfortunately, I haven't had any success yet. If anyone is ab ...

Element that emulates a different element in HTML

Is it possible to have one element that can control and change multiple clone elements, mimicking every change made to the original? While JavaScript & jQuery can easily achieve this, most solutions involve separate variables or instances for each element ...

The data in the viewmodel remarkably aligns with the data stored in $sessionStorage

I have a scenario where I store a list of objects in the $sessionStorage. In one of my controllers, I retrieve this list and display it on the page, allowing the user to delete items from it. The issue arises when an item is deleted from the view model as ...

Adding values to a knockout data table without refreshing the page

I am currently experiencing difficulties with refreshing my knockout.js DataTable. I have a data table that loads correctly on page load using the attached function method. I also have multiple buttons (Display All, Allowed, Not Allowed) that display the t ...

Retrieve the toggle input value to change the page view using jQuery

I'm currently working on a web photo gallery project and I am looking to incorporate a toggle input button that allows users to switch between displaying albums or all photos. To achieve this, I need to extract the value of the input using jQuery and ...

What could be the reason for the malfunctioning of my express delete request?

When I send a delete request using Postman on my localhost, everything functions correctly. However, when trying to make the same request from my React.js client-side, it doesn't go through. Below is the API request: router.delete("/deletetransaction ...

What are the benefits of using `terminal: true` instead of simply deleting lower priority directives?

Related: How to comprehend the `terminal` property of directive? What is the rationale behind setting terminal: true and specifying a priority on a directive rather than removing lower priority directives? For instance, one could do: <tag directive-1 ...

Utilize React to showcase all stored items in localStorage in a Material UI List

I have a storage system with multiple items stored in it. I am looking to retrieve all of them and showcase them using a <ListItem> from Material UI. This is my current code snippet: function saveItem(key, value) { localStorage.setItem(key, value) ...

Store a JSON object without creating a reference to it

My issue is presented in a much simpler manner. Here is the json (you can view it here if you wish) {"resource":[{"id":"1408694994","obj":[{"id":"1","action":[{"name":"ON","id":"301"},{"name":"OFF","id":"302"}]},{"id":"2","action":[{"name":"ON","id":"303 ...

Loader successfully resolving deep array references

My schema is structured as follows: type User{ id: String! name: String posts: [Post] } type Post { id: String! userId: String body: String } I'm utilizing Facebook's dataloader to batch the request. query { GetAllUser { id ...

Tabulator: the process of loading an extensive amount of data requires a significant amount of time

Currently, I am encountering an issue with loading data using a tabulator on my webpage. There are 38 tables that need to be populated, each containing approximately 2000 rows of data. The problem lies in the fact that it is taking an excessive amount of t ...

The attempt to create the maq module was unsuccessful

I've hit a roadblock and can't seem to identify what I'm doing incorrectly in this code snippet. //app.js 'use strict'; var app = angular.module('maq', [ 'ui.router', 'maq.home.controller' ]).ru ...

Exploring the power of Angular's $observe and enhancing it with

Currently, I am closely monitoring attribute values by using $observe within a directive. The callback function is triggered when the value of the attribute is changed. However, when I try to update a $scope variable, it does not seem to reflect the change ...

The specified selector is invalid or illegal in HTMLUnit

Attempting to mimic a login using htmlunit has presented me with an issue despite following examples. The console messages I have gathered are as follows: runtimeError: message=[An invalid or illegal selector was specified (selector: '*,:x' erro ...