0

I have a lightning combobox with data set up kind of like:

[
    {
        'label':'blah',
        'value':'bleh',
        'otherThing':'test',
        'finalThing':'another test'
    },
    {
        'label':'blah2',
        'value':'bleh2',
        'otherThing':'test2',
        'finalThing':'another test2'
    }
]

The combobox applies the label to what you see in the drop down and when something is selected, which is good. And the value is in combobox.get('v.value'), which is also good.

But can I get my other items in the combobox option, like otherThing and finalThing?

Simply doing combobox.get('v.otherThing') did nothing. Is the only way to get anything other than the value (even label, even though we can see it) is to always have to store the options in an aura:attribute list and iterate over them to match values?

Being able to actually get the entire option object instead of just the value seems like a simple thing that should be there, but perhaps it isn't? Is it possible to get the entire object of the selected option in a lightning combobox?

  • do you store your object(s) in a map (some attribute?) you can then fetch the object from the attribute based on the selection. – glls Sep 7 '18 at 16:40
  • Yeah, I create lightning:combobox and set the items as the options, but because I can't find a way to get more than the current selected value from the combobox, I am forced to store them all in an attribute, and iterate over that attribute to match a property with the combobox value. Is this the only way they allow you to do this? Lightning components are nice... but it seems like are a bit half baked. – Tyler Dahle Sep 7 '18 at 16:48
  • i dont see whats wrong with storing objects in attributes and fetching from them, you could try storing each individual object as a string in one of the element attributes, then I guess you could do something similar to event.getSource().get('v.attribute); and then parse it JSON.parse(string) . Maybe someone else can provider better insight as to what the best approach is =) – glls Sep 7 '18 at 16:58
1

Unfortunately, yes, you would need to grab the list and filter the values from the list. Something like this:

let opts = cmp.get("v.options");
let sel = cmp.get("v.selected");
let selOpts = opts.filter(opt => sel.indexOf(opt.value) > -1);
  • That is unfortunate... It would be so convenient to just be able to get the entire option object from the combobox. I did just think of a solution that is now working very nice for me: Turn value into a JSON string of all the extra info I need, so I just get the value, parse it and have all the extra data I need without having to store it in an attribute and parse through it. – Tyler Dahle Sep 7 '18 at 18:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.