Good evening,
I've been having issues with the dropdown menu, particularly with organizing by groups. First of all, it took some time to understand that the array used in options to populate the dropdown needs to be in a very particular way. Fine, my API now returns what it wants and I am able to display the group headings, the individual items, and I can map the selected item to a variable. Very good, but I am only able to map the label (or the value, I'm not sure) of the selected item, and what I really want is other data I have sent along.
Below is the JSON my Angular component receives (you will see I'm a little confused and have wasted properties... I wasn't sure what PrimeNG wanted, and am trying to adapt):
[
{
"group": null,
"label": "First group",
"value": "First group",
"items": [
{
"id": 1,
"name": null,
"label": "Par unité",
"value": "Par unité",
"description": null
},
{
"id": 2,
"name": null,
"label": "Par employé",
"value": "Par employé",
"description": null
}
]
},
{
"group": null,
"label": "Second group",
"value": "Second group",
"items": [
{
"id": 7,
"name": null,
"label": "Temps entre démande et acceptation",
"value": "Temps entre démande et acceptation",
"description": null
},
{
"id": 9,
"name": null,
"label": "something else",
"value": "something else",
"description": null
}
]
}
]
HTML:
<p-dropdown [options]="clientReports"
([ngModel])="selectedReport" (onChange)="onReportChange($event)" placeholder="Choose a report" [group]="true">
TS:
clientReports: any[];
selectedReport: any;
onReportChange(event) {
this.selectedReport = event.value;
console.log(this.selectedReport);
}
Issue and desired outcome:
I think because of the onChange call, my variable selectedReport now has the value (or the label...) of the, well, selected report (coming from "items" in the JSON). Great, however I will need to reference the id later on and I can't seem to figure out how.
To be clear, I do not want the id to be displayed on the dropdown menu, but I need that information passed along to selectedReport (or, if I need a second variable I don't care, I just need that data).
In the documentation for the dropdown, I see that there are multiple properties such as optionLabel, optionValue, dataKey, etc. Many of these change the displayed value in the dropdown, others I can't seem to see what they do.
Hopefully someone can help me out with this.
Thanks in advance.