简体   繁体   中英

Modify my code, I want to replace select drop down list to input box with autocomplete feature

As I am noob in angular, I really need a guidance like how should I replace this select option drop down list to simple input box . I am fetching the data from api, currently I have limited data in my api so its working fine with select option drop down list but my api will contains lots of data so I dont want my users to scroll and search for the data. Rather I want them to type and get select that item. Here I am sharing my code I implemented Select option drop down method. please modify my code to that input box.

  1. metadata-service.service.ts
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { Pincodes } from 'src/app/Model/Pincodes';
import { baseURL } from 'src/environments/environment';

@Injectable({
  providedIn: 'root'
})
export class MetadataServiceService {

  constructor(private http:HttpClient) { }

  public GetPincodes() : Observable<any> {
    console.log("Calling GetPincodes API")
    return this.http.get("/Metadata/api/metadata/getpincodes") // My API Link
  }

}
  1. API consist of the following data
{
  "program": null,
  "version": null,
  "dateTime": null,
  "success": true,
  "errorCode": null,
  "errorMessage": null,
  "message": "Pincodes read Successfully",
  "data": [
    {
      "pincode": 400708,
      "village": "Airoli",
      "taluka": "Thane",
      "district": "Thane",
      "state": "Maharashtra"
    },
    {
      "pincode": 416007,
      "village": "Kaamba",
      "taluka": "Kalyan",
      "district": "Thane",
      "state": "Maharashtra"
    },
    {
      "pincode": 421102,
      "village": "Atali",
      "taluka": "Kalyan",
      "district": "Thane",
      "state": "Maharashtra"
    },
    {
      "pincode": 421501,
      "village": "Ambarnath",
      "taluka": "Ambarnath",
      "district": "Thane",
      "state": "Maharashtra"
    },
    {
      "pincode": 421503,
      "village": "Ambeshiv",
      "taluka": "Ambarnath",
      "district": "Thane",
      "state": "Maharashtra"
    }
  ]
}
  1. Pincode.ts
export class Pincodes {
    pincode : number
    village : string
    taluka : string
    district : string
    state : string
}
  1. register-page.component.ts
import { Component, OnInit } from '@angular/core';
import { Pincodes } from '../classes/pincodes';
import { MetaDataService } from '../services/meta-data.service';
import { FormBuilder, FormControl } from '@angular/forms';
import { Observable } from 'rxjs';

@Component({
  selector: 'app-register-page',
  templateUrl: './register-page.component.html',
  styleUrls: ['./register-page.component.css']
})
export class RegisterPageComponent implements OnInit {
  observeData: Pincodes[]
  modifiedText: any;
  PinSelected: any = {}


  constructor(private _metaDataAPI: MetaDataService) {

  }

  ngOnInit() {
    this._metaDataAPI.getData().subscribe(data => {
      this.observeData = data;
    });

  }

  onPincodeSelected(val: Pincodes) {
    console.log("value" + val);
    this.customFunction(val)

  }
  customFunction(val: Pincodes) {
    // this.modifiedText = "The Selected Pin :  " + val.pincode +
    //  " and selected District is " + val.village
    this.modifiedText = "Selected Pin : " + val.pincode + "\n" +
      "District : " + val.district + "\n" +
      "Taluka : " + val.taluka


    console.log("Modified Text: " + this.modifiedText);
    console.log("Pincode Selected: " + this.PinSelected);
    console.log("observeData Selected: " + this.observeData);

  }


}

  1. register-page.component.html
<select
        [(ngModel)]="PinSelected"
        (ngModelChange)="onPincodeSelected($event)"
      >
        <option *ngFor="let pin of observeData" [ngValue]="pin">
          {{ pin.pincode }}
        </option>
      </select>
      <div>
        <p>{{ modifiedText }}</p>
      </div>

Modify the above code, let me explain once again

I want to implement autocompletion input box. for example in input box when I type 400708 it should fetch all the data from the api like its village, taluka, district, state.

You can simply use ng-select with typeahead feature, follow the tutorial mentioned here

https://www.freakyjolly.com/ng-select-typeahead-with-debouncetime-fetch-server-response/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM