I'm writing angular7 single page web application using ReactiveForms.
I need to list collection of customers in a searchable dropdown list and for that I'm trying to use ngx-select-dropdown
(https://www.npmjs.com/package/ngx-select-dropdown)
My customer class looks like below
export class Customer{
public id:number;
public name:string;
constructor(cusId:number, cusName:string){
this.id = cusId;
this.name = cusName;
}
}
My component class looks like this
import { Component, OnInit } from '@angular/core';
import { FormGroup, FormBuilder, FormArray } from '@angular/forms';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent implements OnInit {
public myForm: FormGroup;
constructor(private formBuilder: FormBuilder) {
}
ngOnInit() {
this.myForm = this.formBuilder.group({
selectedCustomer: [],
customers: this.formBuilder.array([
new Customer(0, "Andrew"),
new Customer(1, "Steve"),
new Customer(2, "Frank"),
new Customer(3, "Jimmie")
])
})
}
}
My HTML template looks like this
<div [formGroup]="myForm">
<ngx-select-dropdown formControlName="customers"></ngx-select-dropdown>
</div>
What I want is customers dropdown list with following options.
- dropdown should be searchable.
- dropdown should be single select.
- when an item is selected, "selectedCustomer" form control should be updated. (see the "Single Select Dropdown" example in this demo: https://manishjanky.github.io/ngx-select-dropdown/)
I've added a sample project in stackblitz