primeng icon indicating copy to clipboard operation
primeng copied to clipboard

Allow binding to dataKey for p-dropdown instead of entire object

Open USLSRV opened this issue 7 years ago • 2 comments

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.

USLSRV avatar May 17 '19 15:05 USLSRV

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

tobibe avatar Sep 13 '19 22:09 tobibe

See;

https://github.com/primefaces/primeng/issues/4295

cagataycivici avatar Mar 24 '20 21:03 cagataycivici