Last active
August 1, 2021 10:59
-
-
Save ierhalim/5b9865b2bd73fb45928eed95401b260b to your computer and use it in GitHub Desktop.
PrimeNG Dropdown Usage
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <p-dropdown | |
| [options]="employees$ | async" | |
| placeholder="Choose an employee" | |
| [showClear]="true" | |
| [filter]="true" | |
| ></p-dropdown> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { HttpClient } from '@angular/common/http'; | |
| import { Component, OnInit } from '@angular/core'; | |
| import { SelectItem } from 'primeng/api'; | |
| import { Observable } from 'rxjs'; | |
| import { map } from 'rxjs/operators'; | |
| import { EmployeeModel } from './employee.model'; | |
| @Component({ | |
| selector: 'app-root', | |
| templateUrl: './app.component.html', | |
| styleUrls: ['./app.component.css'], | |
| }) | |
| export class AppComponent implements OnInit { | |
| constructor(private readonly httpClient: HttpClient) {} | |
| employees$: Observable<Array<SelectItem<number>>>; | |
| ngOnInit() { | |
| this.employees$ = this.httpClient | |
| .get<Array<EmployeeModel>>( | |
| 'https://my.api.mockaroo.com/employees.json?key=35003520' | |
| ) | |
| .pipe( | |
| map((rawData): Array<SelectItem<number>> => { | |
| return rawData.map((employe) => ({ | |
| value: employe.id, | |
| label: `${employe.firstName} ${employe.lastName}`, | |
| })); | |
| }) | |
| ); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| export interface EmployeeModel{ | |
| id: number; | |
| firstName: string; | |
| lastName: string; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment