Skip to content

Instantly share code, notes, and snippets.

@ierhalim
Last active August 1, 2021 10:59
Show Gist options
  • Select an option

  • Save ierhalim/5b9865b2bd73fb45928eed95401b260b to your computer and use it in GitHub Desktop.

Select an option

Save ierhalim/5b9865b2bd73fb45928eed95401b260b to your computer and use it in GitHub Desktop.
PrimeNG Dropdown Usage
<p-dropdown
[options]="employees$ | async"
placeholder="Choose an employee"
[showClear]="true"
[filter]="true"
></p-dropdown>
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}`,
}));
})
);
}
}
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