Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
USLSRV opened this issue May 17, 2019 · 2 comments
Closed

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

USLSRV opened this issue May 17, 2019 · 2 comments

Comments

@USLSRV
Copy link

USLSRV commented May 17, 2019

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 USLSRV changed the title A Allow binding to dataKey for p-dropdown instead of entire object May 17, 2019
@tobibe
Copy link

tobibe commented Sep 13, 2019

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

@cagataycivici
Copy link
Member

See;

#4295

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants