Data Binding in DropDown

UI Components for Angular
Post Reply
SankarReddy
Posts: 17
Joined: 15 Mar 2016, 08:02

30 Mar 2016, 09:04

Hi,
I should display the values in dropdown But id has to be bind to the property.

Ex: <p-dropdown [options]="cars" [(ngModel)]=carId [(value)]="selectedCar" [filter]="true" style="width:100px">

here we display the cars like (BMW,TATA ) and Id(12) has to be bind to property(carId).Could you please let me know how to achieve this.

cagatay.civici
Prime
Posts: 18478
Joined: 05 Jan 2009, 00:21
Location: Cybertron
Contact:

30 Mar 2016, 11:26

There is no ngModel in dropdown, how does your options look? Please post the code of your options.

SankarReddy
Posts: 17
Joined: 15 Mar 2016, 08:02

30 Mar 2016, 13:06

Ex: Customer{
custType:string;
}
customerTypes=[{"121","Government"},{"122","PrivateSector"},{"121","PublicSector"}]

I have to render customerTypes as dropdown and when user select the value,I have to map the id to custType property(if User choose Government ,121 has to be mapped to custType property).
Please do let me know how can we do this.

cagatay.civici
Prime
Posts: 18478
Joined: 05 Jan 2009, 00:21
Location: Cybertron
Contact:

30 Mar 2016, 15:12

Please try something like this;

Code: Select all

export class Demo {

   customerTypes: SelectItem[];

  customer: Customer = new Customer();

  ngOnInit() {
  this.customerTypes=[
      {value:"121",label:"Government"},
      {value:"122",label:"PrivateSector"},
       {value:"121",label:"PublicSector"}]
   }
}

Code: Select all

<p-dropdown [options]="customerTypes" [(value)]="customer.custType" [filter]="true" style="width:100px">

Post Reply

Return to “PrimeNG”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 16 guests