Allow binding to dataKey for p-dropdown instead of entire object
I'm submitting a ... (check one with "x")
[ ] bug report => Search github for a similar issue or PR before submitting
[x ] feature request => Please check if request is not on the roadmap already https://github.com/primefaces/primeng/wiki/Roadmap
[ ] support request => Please do not submit support request here, instead see http://forum.primefaces.org/viewforum.php?f=35
Current behavior Currently when using a p-dropdown the entire object being selected must be bound to ngModel or formControl.
Expected behavior I would like the ability to bind to the dataKey property. e.g. if my dataKey is "id" and I have bound an ngModel or formControl to the p-dropdown I would like the ability to bind to the "id" property from the selected object instead of the full object. Perhaps this could be accomplish with some sort of [bindToDataKey] boolean property or something like that.
Hello USLSRV,
I found a little dirty solution to this, after spending hours and hours researching and debugging:
First import SelectItem from primeng
import { SelectItem } from 'primeng/components/common/selectitem';
Then create your options-Object for the dropdown in you component.ts:
myOptions: Array<SelectItem> = [
{label: 'New York', value: 'NY'},
{label: 'Rome', value: 'RM'},
{label: 'London', value: 'LDN'},
{label: 'Istanbul', value: 'IST'},
{label: 'Paris', value: 'PRS'}
]
Now apply it to your dropdown:
<p-dropdown [options]="myOptions" [(ngModel)]="myModel"></p-dropdown>
Mystically ;-) , now you get in myModel always only the value of SelectItem and not the whole SelectItem-Object.
I stumbled on this by viewing the source code set options(val: any[]) in the p-dropdown
src/app/components/dropdown/dropdown.ts
Hope this helps
Greetings tobibe
See;
https://github.com/primefaces/primeng/issues/4295