2

How to set PrimeNG p-dropdown selected option with dynamic value ?

I am using Formgroup, Formcontrol and my dropdown are set with data querying from database. Now in one of my edit component page I want selected option with dynamic value.

Antikhippe
  • 6,316
  • 2
  • 28
  • 43
Arunava
  • 81
  • 1
  • 1
  • 5

2 Answers2

4

Get the dynamic list of values and push it to the drop down. Below is an example.

data.dtOrgList contains list of values from service layer.

    html
    ------------------
    <p-dropdown [options]="orgs" [(ngModel)]="selectedOrg"></p-dropdown>

    component.ts
    -------------------

     orgs:SelectItem[];
     selectedOrg : string;
     dtOrgList: Array<any>;

    this.itemDetailsService.getItemDetails(input).subscribe(
                data => {
                    this.dtOrgList = data.dtOrgList;
                    this.orgs = [];
                    this.orgs.push({label: 'Select', value: null});
                    for(var i = 0; i < this.dtOrgList.length; i++) {
                        this.orgs.push({label: this.dtOrgList[i], value: this.dtOrgList[i]});
                    }
                    })
Jan69
  • 1,109
  • 5
  • 25
  • 48
0
  1. After you receive the values from the database, set the options of the dropdown with the array of SelectItems.

  2. Set the formControl value associated with the dropdown with the value of the SelectItem.

Lionel
  • 21
  • 2