Searching for data based on specific keywords in Angular 2, rather than using a wildcard search, can be done by utilizing the key-in

My dropdown contains 100 values, and I am currently able to search for these values based on key input using wild search. However, I would like the dropdown to display values based on the specific alphabet that I enter first.

HTML:

  
<div class="col-sm-6 pull-left m-b10 m-t10">
    <label class="lbl_manifes pull-left col-form-label g-color-gray-dark-v2 g-font-weight-700 text-sm-left no-padd">Manifestation
      <span class="required">*</span>
    </label>
    <div class="col-sm-8 pull-left no-padd w-71">
      <input type='text' (keyup)="searchDropDown(175, src13.value)" #src13 formControlName="Manifestation" [(ngModel)]="selectedManifestation" placeholder="Please keyin (eg:Ac)"
      />
      <i class="fa fa-caret-down"></i>
      <div *ngIf="allergyDropDown && allergyDropDown?.manifestation && IsHidden" class="emr-dropdown">
        <ul *ngFor="let manifestationType of allergyDropDown?.manifestation" (click)="getValue(manifestationType)" class="p-l10 m-b0 brd-b">
          <li>
            {{manifestationType.Description}}
          </li>
        </ul>
      </div>
    </div>
  </div>

TS:

  
  public getValue(item) {
    this.IsHidden = false;
    if (item.CategoryId == 175) {
      this.selectedManifestation = item.Description;
    }
  }

  public searchDropDown(Id, desc) {
      let params = { Id, desc: desc }
      this.emrService.getDropDown(params)
        .subscribe((res) => {
          this.IsHidden = true;
          this.initializeAllDropDown(res.Body.Data)
        })
  }

Answer №1

I have made some updates to your function in order to display only the closest search result. I have added filtering based on the item's description field, but you can specify the actual column name you wish to use for filtering. This modification should help you resolve any issues with your search functionality. Please replace your current searchDropdown function with the following code snippet,

public searchDropDown(Id, desc, index) {
    let params = { Id: Id, desc: desc }
    this.emrService.getDropDown(params)
      .subscribe((res) => {
        this.IsHidden = true;
        this.contactIndex = index;
        var data = res.Body.Data.filter(item => item.desc.toLowerCase().indexOf(desc.toLowerCase())==0);
        this.initializeAllDropDown(data)
      })
}

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

The implementation of getStaticPaths was done independently of getStaticProps - TypeScript

I am currently in the process of setting up a new blog using a combination of nextJS, TypeScript, and sanity CMS. The homepage is already set up to display posts perfectly. Next on my list is to display the details of each post when it is clicked, based on ...

What is the best way to include a string index signature in a preexisting Array?

Currently, I am immersed in styled-system and attempting to define a pattern that is frequently employed within the library. const space: { [key: string]: string } = [ '0.25rem', '0.5rem', '1rem', '2rem', ...

Building a hierarchical tree structure using arrays and objects with Lodash Js

I am attempting to create a tree-like structure using Lodash for arrays and objects. I have two arrays, one for categories and the other for products, both with a common key. The goal is to organize them into a tree structure using string indexing. let ca ...

What is the impact on active subscriptions when the browser is closed or refreshed?

Within the app component (the root component) of an Angular application, I have a few subscriptions set up. Since the app component remains active and is never destroyed until the application is closed, the ngOnDestroy method of the app component does not ...

Ways to obtain the scrollWidth and offSetWidth of a div that is being utilized as an ngx-datatable-cell-template within an ngx-datatable

How can I retrieve the scrollWidth and offSetWidth of a div that is included in a child component being loaded as an ngx-datatable-cell-template inside ngx-datatable? I am consistently receiving values of 0. I have added a template variable for the div el ...

Regular expression for textarea validation

I'm currently working on creating a regex for a textarea in my Angular 8 application. The goal is to allow all characters but not permit an empty character at the start. I've experimented with 3 different regex patterns, each presenting its own s ...

What is the best way to store the output of a function in a local variable?

In my Type Script code, I am looking to store the return value of a function in a local variable. The process is outlined below: getdetail1(store){ let Cust_id=this.sharedata.latus_lead.m_type let url="http:domain.com" console.lo ...

Determining function return property type in Typescript by mapping interface argument property

In order to manage messaging between the browser and a web worker, I have developed a generic class. Each side creates a class that can send specific messages and acknowledge them on the other side with a returned result in a payload. The implementation is ...

Expanding MaterialUi styled components by incorporating TableCellProps: A guide

When trying to create a styled TableCell component in a separate file, I encountered an error in TypeScript (ts(2322)). TypeScript was indicating that the properties "component," "scope," and "colSpan" could not be used because they do not exist in StyledC ...

Setting the background color of a button within a template in an Angular 8 component using style.background

I have been exploring the different versions of Angular and their changes. Currently, I am enrolled in an Angular course on Udemy where I have installed Angular 8. In the course, it is mentioned to use style.backgroundColor on a button inside the template ...

Having Trouble with Ionic 2's Loading Controller

In my attempt to utilize the recently added LoadingController in this scenario: let loading=this.load.create({ content: "Connexion au serveur Migal en cours..." }); loading.present(); this.http.get(this.urlCheckerForm.value.migalUrl+'/action/Mobi ...

Instead of relying on the valueChanges method, consider using setValue or patchValue in Angular 4 to monitor

I've implemented a floating point directive on fields in a reactive form to enhance readability by adding commas every 1000 and appending .00 to field values. This formatting works well, with onBlur triggering the formatting and onFocus removing it. ...

React component not displaying any content due to ternary operator condition being met with variable equal to 0

Seeking to display a React component upon clicking another component. When clicked, I assign the eventKey of the component to a savedId in order to render the corresponding data from the array at that index. Click Action <ListGroup> {data.map((it ...

Issue: ALERT found in ./src/styles.scss (./node_modules/raw-loader!./node_modules/postcss-loader/lib?

Hello to all my fellow developers! I've been diving into the Angular 6 project recently and things are going smoothly for the most part. However, I keep encountering a warning in the console after running the application with (ng serve). WARNING i ...

How to define a TypeScript recursive object with a defined endpoint?

Welcome to my first question! I am currently facing an issue with defining an object to store strings in multiple languages. I am looking for a flexible solution and have considered using a nested object structure. However, I want the final object to adhe ...

What is the process for defining the type or interface of an object in Visual Studio Code?

Is there a way to create a new type or interface by replicating the structure of a complex object that is imported from a library? For instance, in the image below, the object Text is taken from react-three/drei. Upon inspecting the object, I see that it ...

Is it possible to confirm that a value is a valid key without prior knowledge of the object's keys during compile-time?

Is there a way in TypeScript to declare that a variable is a keyof some Record without prior knowledge of the keys? For instance, consider an API response returning JSON data. Is it possible to define a type for the keys of this payload to ensure that whe ...

Enable the Angular button only when the radio button has been selected

Just starting out with Angular and I have a query. This is a scenario for my project at work. https://i.stack.imgur.com/R3SxA.png In this screenshot, I want to enable the "ajouter" button when at least one radio button is selected in the secure chest (s ...

Retrieve an array of information and convert it into an object using Angular

My previous data is displaying correctly in the chart, as shown below: @Component({ selector: 'app-inpout-bar-chart', templateUrl: './inpout-bar-chart.component.html', styleUrls: ['./inpout-bar-chart.component.scss'] }) exp ...

Ways to prevent encountering the "ERROR: Spec method lacks expectations" message despite achieving success

My Angular HTTP service is responsible for making various HTTP calls. Below is a snippet of the service implementation: import { Injectable } from '@angular/core'; import { HttpClient } from '@angular/common/http'; @Injectable() expor ...