dropdown with an array of string ?

UI Components for Angular
Post Reply
antigone
Posts: 7
Joined: 15 May 2018, 16:38

25 May 2018, 10:56

Hi,
i wanted to know if you have an example of how you would code a dropdown with an array of string representing the options.
I would like to avoid to turn this array in a array of object. is it possible ?

Thank you

crazyjay
Posts: 1
Joined: 29 Mar 2021, 19:33

29 Mar 2021, 19:39

I just posted this answer at stackoverflow: Getting PrimeNG p-dropdown to work with array of strings is possible, though it is not well documented. I use it sometimes when selecting timezones. There might be cleaner options, but I use ng-template to populate the dropdown and onChange to store the selected string value:

HTML

Code: Select all

<p-dropdown [options]="portNames" placeholder="Select a Port" (onChange)="storeValue($event)">
  <ng-template let-item pTemplate="selectedItem"> 
    {{item}} 
  </ng-template> 
  <ng-template let-item pTemplate="item"> 
    {{item}}
  </ng-template>
</p-dropdown>
Component.ts

Code: Select all

portNames: string[] =["Port 01","Port 02", "Port 03"];    

selectedPort="";

storeValue(event) {
    console.log(event); //event.value will likely be undefined, check event.originalEvent
    this.selectedPort = event.originalEvent.srcElement.innerText;
}

Post Reply

Return to “PrimeNG”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 20 guests