PrimeNG dropdown selected option with dynamic value

10,568

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]});
                    }
                    })
Share:
10,568

Related videos on Youtube

Arunava
Author by

Arunava

Updated on June 04, 2022

Comments

  • Arunava
    Arunava almost 2 years

    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.

  • SamuraiJack
    SamuraiJack about 6 years
    You saved my life ! :D